admin管理员组文章数量:1323335
I'm writing a HOC ponent for next.js pages, and this HOC needs to accept a ponent with a specific getInitialProps
static function.
I can't get figure out the right typing for this with 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} />;
}
}
}
I get this error:
5: const original: Function = Component.getInitialProps;
^ property `getInitialProps`. Property not found in
5: const original: Function = Component.getInitialProps;
^ statics of React$Component
Demo
I'm writing a HOC ponent for next.js pages, and this HOC needs to accept a ponent with a specific getInitialProps
static function.
I can't get figure out the right typing for this with 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} />;
}
}
}
I get this error:
5: const original: Function = Component.getInitialProps;
^ property `getInitialProps`. Property not found in
5: const original: Function = Component.getInitialProps;
^ statics of React$Component
Demo
Share edited Sep 26, 2017 at 7:12 Simon Boudrias asked Sep 26, 2017 at 5:38 Simon BoudriasSimon Boudrias 44.7k16 gold badges108 silver badges136 bronze badges 3-
React ponents never had an
getInitialProps
method. If those are specific ponents then you have to typeComponent
as such, not asReact.Component
. – Felix Kling Commented Sep 26, 2017 at 5:45 - @FelixKling you mean create a class interface? – Simon Boudrias Commented Sep 26, 2017 at 7:11
- That would work I guess. – Felix Kling Commented Sep 26, 2017 at 7:35
1 Answer
Reset to default 4Is this what you're looking for?
// @flow
import React from 'react';
import type {ComponentType} from 'react';
interface StaticInterface {
fn(): void;
}
class NoStatic extends React.Component<{}> {
}
class WithStatic extends React.Component<{}> {
static fn() {}
}
const c1: ComponentType<{}> = NoStatic;
const c2: ComponentType<{}> = WithStatic;
const c3: ComponentType<{}> & StaticInterface = WithStatic;
// const c4: ComponentType<{}> & StaticInterface = NoStatic; // NOT OK
(demo)
I find this pretty confusing myself. I adapted it from this:
https://blog.bluematador./posts/enforcing-method-presence-in-react-ponents-with-flow
Related:
https://github./facebook/flow/issues/2048
https://github./facebook/flow/pull/3994
本文标签: javascriptHow to type a ReactComponentType with a static propertyStack Overflow
版权声明:本文标题:javascript - How to type a React.ComponentType with a static property? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742137933a2422452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论