1

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 ?

1
  • Not the right place to ask this but, in general it is a good idea to use prop-type checking in every component, as long as it takes props, regardless of whether or not it's stateless or not. Commented Feb 7, 2019 at 19:19

2 Answers 2

1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.