How to use Prop Types in React ?

 Why to use Prop Types in React ?

Props are used to pass down the data from the parent component to the child component but as our application grows the props we pass to the child components sometimes validate wrong and we get the wrong result also it hard to debug where we are lacking.

For example we want age in number datatype in the child component instead we pass age in the string data types so, we get the wrong result just by the wrong data type we passed.

So, in this type of case we need validation to ensure we use correct datatype for the props and we can also ensure what kind of data we want to put in the prop whether default or must have values.

How and When to use Prop Types in React ?

When working with functions , we pass arguments into the functions and when we pass wrong argument , it throws an error . Just like , we want to know where we are doing the wrong validation so we can correct at the point and specify those validation into our react app . In this case , the prop types is the best suited for performing the validation in the react app.

Initially the prop types is the built in module in the react , but after the react updates this is the different package and needed to be installed.
 
You can install into project by running this command into the project


Once , it has installed now you can import it into the component and use in the project


After installing and importing prop-types in react , it will be attached to component name where you import with the dot notation and in the object you can pass key value pair for prop validation


In the prop-types object you can pass key value pair where the key will be the prop name and value will the prop-type validation which you can pass.
Now ,let's dive into what kind and types of validation you can pass into this object

Types of Validation

1. Basic Types

Basic Prop-types validation where you can pass which type of datatype prop can hold whether string , bool , number , array , object , symbol and any datatype . 


If the prop-types validation  fail , it will throws the error and you must need to pass the prop in the datatype for the validation you have passed.


You can also required the prop in the validation , which means if prop is not passed into the child component it will throws the error and you need to pass the prop to the component .


2. Multiple Types

Props types can also include the what types of values you can include in the array and can also specify the datatype which you want to include in the the array.

The first one is oneOf method of the prop types which ensure which value want to include in the array .


If you specify any value rather than the specified one you get the error .

The second one is PropTypes.oneOfType which specify the datatype you want to include in the array and you specify rather than the specified datatype , you get the error.



3. Collective Types

In this type , you can specify which type of data types you want to validate the array or objects . If you want to specify only number datatype in the array and not the string where you can validate these types of conditions.


If , number is passed into the string with some type error , you will get an error.
You can also do this same with object as you can specify which types of data type you can specify in the object value and if you pass string to the value to the object and you validate it with the number then , you will face an error.


If you will pass some other data type into the object value , then you will face an error.

4. Default Prop-type

In this, default prop-type you can specify any value to the prop values . These values will be passed when no prop will be passed to the component.


Note :- This will only be passed when the userDetails prop will not be passed to the component.

Conclusion

In this article , you have learned about the prop-types and the basic validators methods and validate through any kind you wanted.

Feel free to code and learn daily by the daily blog post.

Thank you for reading :)



Comments

Popular posts from this blog

Learn React Router v6 ( Part - 2)

Why use Typescript ?

What is Single Page Application Routing ?