How can I use the flowtype method to determine support in the context of component response?

When using flowtype, define the prop in context 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: the specification type of the child context of someProps invalid; type checking function should return null or Error but returned an object. You may have forgotten to pass the argument to the creator of the type check (arrayOf, instanceOf, objectOf, oneOf, oneOfType and shape all require an argument).

Therefore, I am forced to use propTypes.object instead of Object .

+6
source share
1 answer

No, you cannot use a stream type as a type in childContextTypes . The reason is that childContextTypes checked at runtime, and stream types have no representation at runtime. Therefore, the "types" in childContextTypes must be values ​​- in particular, the values ​​provided by the prop-types npm package.

Using MyType in your example is actually a syntax error: MyType appears at the position of the property value of the object, which should be a value, not a type.

+4
source

Source: https://habr.com/ru/post/1015185/


All Articles