What is the Flow equivalent of React.PropTypes.node
(i.e., anything that can be rendered by React, if there is one? Do I have to create it myself as a union type?
In other words, what would replace ???
here?
type Props = {
children: ???,
}
const UselessComponent
: (props: Props) => React$Element<*>
= ({ children }) => (
<div>
{children}
</div>
)
UselessComponent.propTypes = {
children: React.PropTypes.node.isRequired,
}
PropTypes. node: any render-able value like numbers and string, that can actually be rendered on screen. PropTypes. any: any type of value, even those are non-render-able like boolean.
PropTypes provide run-time type checking in the browser console, while Flow types provide compile-time type-checking.
PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.
React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead. We provide a codemod script to automate the conversion. As your app grows, you can catch a lot of bugs with typechecking.
It looks like it is still an issue here.
According to the discussion in that issue, what you should do until it is fixed:
type Props = {
children?: React.Element<*>,
};
For versions of flow >= 0.53, use the new type React.Node
for props.children and anywhere you are expecting a renderable node.
The definition of React.Node can be roughly approximated with a React.ChildrenArray:
type Node = React.ChildrenArray<void | null | boolean | string | number | React.Element<any>>;
There was a major rework of the react types in flow 0.53. Summary of the changes and instructions on how to migrate are in the release notes. The flow docs explain how to use in detail.
For example:
import type { Node } from 'react';
type Props = {
input?: Node,
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With