Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is it possible to use flowtype to define a prop in react context of a component?

When using flowtype to define a prop in the context in this way

// @flow
type MyType = Object;
class CustomView extends React.Component {
    static childContextTypes = {
        someProp: MyType
    }
    getChildContext() {
        return {
            someProp: this.props.someProp
        };
    }
}

I got the following error:

CustomView: type specification of child context someProps is invalid; the type checker function must return null or an Error but returned a object. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).

So I am forced to use propTypes.object instead of Object.

like image 258
locropulenton Avatar asked Feb 22 '17 15:02

locropulenton


1 Answers

No, you cannot use a Flow type as a type in childContextTypes. The reason is that childContextTypes is checked at runtime, and Flow types do not have any runtime representation. Therefore the "types" in childContextTypes must be values - specifically the values provided by the prop-types npm package.

The use of MyType in your example is actually a syntax error: MyType appears in the position of an object property value which must be a value, not a type.

like image 142
Jesse Hallett Avatar answered Sep 28 '22 17:09

Jesse Hallett