react#ComponentType JavaScript Examples

The following examples show how to use react#ComponentType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: asyncComponent.js    From surveillance-forms with MIT License 6 votes vote down vote up
ComponentType<Props> {
  class AsyncComponent extends Component<Props, State> {
    constructor(props: Props) {
      super(props);

      this.state = {
        component: null
      };
    }

    componentDidMount() {
      this.fetchComponent();
    }

    async fetchComponent() {
      const { default: component } = await loadComponent();

      this.setState({ component });
    }

    render() {
      const C = this.state.component;

      return C ? <C {...this.props} /> : null;
    }
  }
Example #2
Source File: SocketConnect.js    From flatris-LAB_V1 with MIT License 5 votes vote down vote up
export function withSocket(
  CompType: ComponentType<*>,
  syncActions: {
    [propName: string]: (...args: any) => JoinGameAction | ThunkAction
  } = {}
) {
  class SocketConnect extends Component<Props> {
    static displayName = `SocketConnect(${CompType.displayName ||
      CompType.name ||
      'UnnamedComponent'})`;

    static contextTypes = {
      subscribe: func.isRequired,
      keepGameAlive: func.isRequired,
      broadcastGameAction: func.isRequired,
      onGameKeepAlive: func.isRequired,
      offGameKeepAlive: func.isRequired
    };

    createActionHandler = (actionName: string) => async (...args: any) => {
      const { broadcastGameAction } = this.context;
      const actionCreator = syncActions[actionName];

      // NOTE: This must only run on the client!
      return broadcastGameAction(actionCreator(...args));
    };

    getBoundHandlers() {
      return Object.keys(syncActions).reduce((acc, actionName) => {
        return {
          ...acc,
          [actionName]: this.createActionHandler(actionName)
        };
      }, {});
    }

    render() {
      return (
        <CompType
          {...this.props}
          {...this.context}
          {...this.getBoundHandlers()}
        />
      );
    }
  }

  return SocketConnect;
}