react js-tutorial

State Vs. Props

State

The state, which can change over time, is a structure that can be updated and is used to store data or information about the component. The status can change in response to a system event or user activity. The react component’s core is what controls the component’s functionality and how it renders. A state must remain as straightforward as feasible. It represents the local state or data of the component. Only the component itself or the component itself can access or modify it.

Props

As read-only components, props. It is an object that functions similarly to HTML attributes in that it stores the value of a tag’s attributes. It enables the transfer of data between components. It can be supplied to the component in the same manner as function parameters and is similar to those arguments. Since props are immutable, we are unable to change them from within the component.

What happens when state changes?

OK, why must we use setState( )? Why do we even need the state object itself? If you’re asking these questions, don’t worry – you’ll understand state soon 🙂 Let me answer.

A change in the state happens based on user-input, triggering an event, and so on. Also, React components (with state) are rendered based on the data in the state. State holds the initial information.

So when state changes, React gets informed and immediately re-renders the DOM – not the whole DOM, but only the component with the updated state. This is one of the reasons why React is fast.

And how does React get notified? You guessed it: with setState( ). The setState( ) method triggers the re-rendering process for the updated parts. React gets informed, knows which part(s) to change, and does it quickly without re-rendering the whole DOM.

In summary, there are 2 important points we need to pay attention to when using state:

  • State shouldn’t be modified directly – the setState( ) should be used
  • State affects the performance of your app, and therefore it shouldn’t be used unnecessarily

Can I use state in every component?

Another important question you might ask about state is where exactly we can use it. In the early days, state could only be used in class components, not in functional components.

That’s why functional components were also known as stateless components. However, after the introduction of React Hooks, state can now be used both in class and functional components.

If your project is not using React Hooks, then you can only use state in class components.

What are the differences between props and state?

Finally, let’s recap and see the main differences between props and state:

  • Components receive data from outside with props, whereas they can create and manage their own data with state
  • Props are used to pass data, whereas state is for managing data
  • Data from props is read-only, and cannot be modified by a component that is receiving it from outside
  • State data can be modified by its own component, but is private (cannot be accessed from outside)
  • Props can only be passed from parent component to child (unidirectional flow)
  • Modifying state should happen with the setState ( ) method

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published.