Defines the data type for props in ReactJS

Tram Ho

In React code, React other people’s components is essential. Maybe many of you have had a problem about what this prop is and what to pass to it is appropriate. Therefore, defining the data types for those props is essential so that other people using that component can understand them without having to read the code.

1. What is PropTypes?

PropTypes understands a simple way to help you check the props passed to the component of what type is.

When others use your component , they will easily know what that props is and whether to pass it in or not.

You can also set a default value for props through defaultProps .

2. How to use it?

To use it is extremely simple you just need:

or

After that:

3. What are the types of PropTypes?

To force it to pass in a prop, just add isRequired after the type.

4. How do I pass default values ​​to props?

It is very simple, you just need to declare more defaultProps

5. Conclusion

Above is some knowledge about prop-types that I have learned. Hope it helps you

Reference link: https://reactjs.org/docs/typechecking-with-proptypes.html

Share the news now

Source : Viblo