moaible
4/14/2017 - 7:54 AM

Typed React Component

Typed React Component

/* @flow */
import React from 'react'

export interface ReactComponentable<T, U> {
  props: T;
  state: U;
  configureStateFor(props: T): ?U;
}

export class TypedReactComponent<T, U> extends React.Component<*, *, *> implements ReactComponentable<T, U> {

  static defaultProps: T;

  props: T;

  state: U;

  constructor(props: T) {
    super(props)
    let state = this.configureStateFor(props)
    if (state) {
      this.state = state
    }
  }

  configureStateFor(props: T): ?U {
    return null
  }
}