Reputation: 821
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 ?
Upvotes: 1
Views: 91
Reputation: 360
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
}
Upvotes: 1
Reputation: 20634
When should you use them? Whenever you want. Might be nice to add them if you're sharing these components with other developers (at work or on npm)
Can you use them in both functional and class components? Yes.
Upvotes: 1