I have a couple question about prop-types in React: 1. when should we use react props-type checking in component, do we have to use it in all component with props passed in ? 2. can props-type checking be applied in both stateless component and class component ?
2 Answers
You can use prop-types in whatever components you want prop validation to occur. It doesn't have to be used in every component that uses props, although it's generally a good idea to do so.
For a class component, you can do:
class Component extends React.Component { static propTypes = { // prop types here } // component stuff } For a functional component, you can do:
const Component = (props) => { // component stuff } Component.propTypes = { // prop types here }