Props in React Js
Introduction
In React , the modularity of components where all the logic is separate and not dependent on each other but as you know, the parent component and child component are connected as the child component called in the parent component . When they wanted the values from parent component , they will just use props to pass the state between the components. Props play a important role in passing data from one component to another component and that's how modularity achieves.
Props are the html attribute that takes the value in the left side where you can also pass the state in the jsx object and any value that is achieved in the parent component. By it's name it is properties which in short called props as they play a role like html properties which used as the properties of component.
As, you can see in the picture below the addNewTodo is the value which is passed to addNewTodo prop that will be accessed in the Todo component by the argument.
You will access the properties just the way you access the value of objects . Inside the component , props are accessed as a object where you can use the value from the parent component . Props are read only values and they cannot be modified and mutated in the child component and will only be read in the child component.
Props are like the parameter we pass parent to child and the component access them by the arguments.
Props VS State
Many developers are confused between the prop and state they are both are used for state management and to give value to component so that the value displayed in the web.
1. Props cannot be modified or mutable whether state can be modified or updated
2. Props can be used in functional and class components and state can also be used in both earlier state can only be used in class components.
3. Props are generally used for the pass the data from parent to child whether state is used to store the data of the component that have to rendered to the view.
4. Updation of props are from parent to child whether state are generally updated by used interaction ( event handlers) .
Props Drilling or Prop Chaining
Prop Drilling is a concept where there is a chain of parent and child component . For example , you father , then grandfather , grand grandfather , grand grand grandfather etc. Where there is large chain of components and we need to pass some value from top level component to the lowest component in the chain , then we need to pass that prop to every component between them event they are not using the value , they need to access the value to pass the value to child component . This scenario is called prop drilling . To prevent this , we have a concept of context api that frees us from prop drilling and we don't need to pass data form every component.
Conclusion
Prop is a very concept to understand when you are starting to learn react js , where the react is only know about its modularity of components and state management which cannot be achieved without the props.
Thank you for reading :)
Comments
Post a Comment