如何使用流键入更高阶的组件

时间:2017-01-25 21:22:27

标签: reactjs flowtype

我有以下HOC:

export default (keys: Array<string>) =>
  (WrappedComponent: React.Component<*, *, *>) => (props: Object): React.Element<*> => {
    if (hasNotYetLoadedProps(keys, props)) {
      return (
        <div
          style={{
            textAlign: 'center',
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100%',
          }}
        >
          <div>Loading</div>
        </div>
      )
    }
    return <WrappedComponent {... props} />
  }

要么渲染原始组件,要么渲染加载指示器。使用实际的流类型声明我收到此错误:

React element `WrappedComponent`. Expected React component instead of React$Component 

在最后一行。输入组件的正确类型是什么?

1 个答案:

答案 0 :(得分:5)

类型需要ReactClass<any>

export default (keys: Array<string>) =>
  (WrappedComponent: ReactClass<any>) => (props: Object): React.Element<*> => {
    if (hasNotYetLoadedProps(keys, props)) {
      return (
        <div
          style={{
            textAlign: 'center',
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100%',
          }}
        >
          <div>Loading</div>
        </div>
      )
    }
    return <WrappedComponent {... props} />
  }