0

我遇到了一个问题,我可以将一个类或一个功能组件传递到我的 HOC 中。在 HOC 中,我需要访问正在传入的组件上的静态属性(PAGE)。这里的问题是,FLOW 给了我一个错误,说它找不到正在传递给 HOC 的 PAGE 上的静态属性:

Error:(50, 21) Flow: property getInitialProps. Property not found in statics of React$Component.

该错误来自以下代码中的这一行:

const pageProps =
            (await Page.getInitialProps) && (await Page.getInitialProps(ctx))

任何帮助将不胜感激 - 我的全部内容:

type DefaultProps = {
      getInitialProps: (ctx:any) => any
    }

type FunctionComponent<P> = (props: P) => ?React.Element<*>;
type ClassComponent<D, P, S> = Class<React.Component<D, P, S>>;

type Component<D, P> = FunctionComponent<P> | ClassComponent<D, P, any>;

export default <D: DefaultProps, P: {}, S: {}> (Page: Component<D, P>, title: string = '') => {
  class standardLayout extends React.Component {
    static async getInitialProps (ctx) {



// Flow can't read getInitialProps
   const pageProps = Page.getInitialProps ? (await Page.getInitialProps(ctx)) : await Page.getInitialProps

      return {
        ...pageProps,
        currentUrl: ctx.pathname
      }
    }

    render () {
      return (
        <div>

          <Page {...this.props} />

        </div>
      )
    }
  }

  return connect()(standardLayout)
}
4

1 回答 1

2

我发现摆脱这个错误的最好方法是检查是否Page.getInitialProps是一个函数。一个简单的typeof Page.getInitialProps === 'function',或者try ... catch为我工作的,Flow 很高兴。但我还是不明白为什么会这样:const pageProps = Page.getInitialProps ? (await Page.getInitialProps(ctx)) : await Page.getInitialProps?您不能这样做await Page.getInitialProps,只需将其替换为nullor{}

于 2017-06-24T13:11:21.187 回答