DEV Community

Barnabas Ukagha
Barnabas Ukagha

Posted on

Difference between props and state in React.js

When learning React.js, some of the first and most important concepts you will learn are props and state. One of the issues faced by beginners is not knowing the difference between props and state and when to use them.

This article will teach us the difference between props and state and when to use them.

Difference between props and state

Props

  • Props are like arguments used to pass data from parent to child components.
  • Props are immutable; they can't change once they've been set.
  • Props are used in both functional and class components.
  • You use this.prop to access props in class components as well as an argument in functional components.

State:

  • A state is like a component memory used to store and change data within a component.
  • State is mutable; its data can change at any time.
  • State is only present in class components. Functional components can only use state using React hooks.
  • You use this.state = {...} to initialize the constructor of a class component and the useState() method to change it.

When to use props and state

You use props whenever you want to parse data from parent to child components. You use state to store and change data within a component.

You can use Props to parse state data from parent to child components.

Conclusion

Now that you know the difference between props and state and when to use them, I urge that you practice using them on many easy projects. You might not get the hang of it at first, but with continuous practice, you will get better and master using them in no time.

Also, learn and practice other React concepts like events, conditionals, and hooks to get better at React.js.
Thank you
And
Happy Hacking.

Top comments (0)