【发布时间】:2018-03-07 05:05:15
【问题描述】:
我正在为 next.js 页面编写一个 HOC 组件,这个 HOC 需要接受具有特定 getInitialProps 静态函数的组件。
我无法通过 flow 找出正确的输入方式:
const wrapComponent = (Component: React.ComponentType<*>) => {
const original: Function = Component.getInitialProps;
return class extends React.Component<*> {
static async getInitialProps(ctx) {
const props = await original(ctx);
return {
...props,
custom: 'a',
};
}
render() {
return <Component {...this.props} />;
}
}
}
我收到此错误:
5: const original: Function = Component.getInitialProps;
^ property `getInitialProps`. Property not found in
5: const original: Function = Component.getInitialProps;
^ statics of React$Component
【问题讨论】:
-
React 组件从来没有
getInitialProps方法。如果这些是特定组件,那么您必须输入Component,而不是React.Component。 -
@FelixKling 你的意思是创建一个类接口?
-
我猜这会起作用。
标签: javascript reactjs flowtype nextjs