How to best define a propType for accepting a component as a prop?
See original GitHub issueGiven this contrived example, what is the best definition of componentType
?
const componentType = PropTypes.oneOfType([
PropTypes.shape({render: PropTypes.func.isRequired}), // React.createClass / React.Component ...better way to describe?
PropTypes.func, // Stateless function
// others?
]);
const Selector = React.createClass({
propTypes: {
components: PropTypes.arrayOf(componentType).isRequired,
index: PropTypes.number.isRequired,
},
render() {
const Component = this.props.components[this.props.index];
return (
<Component />
);
}
});
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:20 (4 by maintainers)
Top Results From Across the Web
Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >reactjs - propType for accepting a component as a prop?
I crossposted to github and got this answer. So it should be: const componentType = PropTypes.oneOfType([PropTypes.string, PropTypes.func]).
Read more >Mastering Props And PropTypes In React - Smashing Magazine
This tutorial will introduce you to the details about props, passing and accessing props, and passing information to any component using props.
Read more >How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >How to Use PropTypes in React - freeCodeCamp
If you want to just check to see if a prop is a React component, you can use PropTypes.element. This is useful for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can now use
PropTypes.elementType
to validate for Component type props:https://github.com/facebook/prop-types/pull/211
@jimbolla
React.PropTypes.node
I believe is the closest one, it means anything renderable.