Managing State in a React App: An Introduction to State and Props

Welcome to this tutorial on managing state in a React app! State and props are important concepts in React that allow you to store and pass data between components in your app. In this tutorial, we will explore what state and props are, how they are used in a React app, and how to manage them effectively.

First, let's define state and props. In React, state refers to the data or variables that determine a component's behavior and render information to the user. Props, on the other hand, are data or variables that are passed to a component from its parent.

Here's an example of how state and props might be used in a simple React app:

Y2xhc3MgTXlDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5Db21wb25lbnQgewogIGNvbnN0cnVjdG9yKHByb3BzKSB7CiAgICBzdXBlcihwcm9wcyk7CiAgICB0aGlzLnN0YXRlID0gewogICAgICBjb3VudDogMAogICAgfTsKICB9CgogIHJlbmRlcigpIHsKICAgIHJldHVybiAoCiAgICAgIDxkaXY+CiAgICAgICAgPGJ1dHRvbiBvbkNsaWNrPXsoKSA9PiB0aGlzLnNldFN0YXRlKHsgY291bnQ6IHRoaXMuc3RhdGUuY291bnQgKyAxIH0pfT4KICAgICAgICAgIEluY3JlbWVudAogICAgICAgIDwvYnV0dG9uPgogICAgICAgIDxwPkNvdW50OiB7dGhpcy5zdGF0ZS5jb3VudH08L3A+CiAgICAgIDwvZGl2PgogICAgKTsKICB9Cn0KClJlYWN0RE9NLnJlbmRlcig8TXlDb21wb25lbnQgLz4sIGRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdyb290JykpOw==

In this example, we have a class-based component called MyComponent that has a state variable called count. The count variable is initialized to 0 in the constructor. The render method of the component returns a button that, when clicked, increments the count variable by 1 using the setState method. The count variable is then displayed to the user using a p element.

Now, let's say we want to pass a prop called initialCount to MyComponent from its parent component. We can do this like this:

Y2xhc3MgUGFyZW50Q29tcG9uZW50IGV4dGVuZHMgUmVhY3QuQ29tcG9uZW50IHsKICByZW5kZXIoKSB7CiAgICByZXR1cm4gPE15Q29tcG9uZW50IGluaXRpYWxDb3VudD17NX0gLz47CiAgfQp9CgpSZWFjdERPTS5yZW5kZXIoPFBhcmVudENvbXBvbmVudCAvPiwgZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ3Jvb3QnKSk7

In this example, we have a parent component called ParentComponent that renders an instance of MyComponent and passes an initialCount prop to it with a value of 5.

We can then update the MyComponent class to use the initialCount prop to initialize the count state variable:

Y2xhc3MgTXlDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5Db21wb25lbnQgewogIGNvbnN0cnVjdG9yKHByb3BzKSB7CiAgICBzdXBlcihwcm9wcyk7CiAgICB0aGlzLnN0YXRlID0gewogICAgICBjb3VudDogcHJvcHMuaW5pdGlhbENvdW50CiAgICB9OwogIH0KCiAgcmVuZGVyKCkgewogICAgcmV0dXJuICgKICAgICAgPGRpdj4KICAgICAgICA8YnV0dG9uIG9uQ2xpY2s9eygpID0+IHRoaXMuc2V0U3RhdGUoeyBjb3VudDogdGhpcy5zdGF0ZS5jb3VudCArIDEgfSl9PgogICAgICAgICAgSW5jcmVtZW50CiAgICAgICAgPC9idXR0b24+CiAgICAgICAgPHA+Q291bnQ6IHt0aGlzLnN0YXRlLmNvdW50fTwvcD4KICAgICAgPC9kaXY+CiAgICApOwogIH0KfQ==

In this updated version of MyComponent, the count the state variable is initialized to the value of the initialCount prop passed from the parent component. The state should only be modified using the setState method, as direct manipulation of the state, can lead to unexpected behavior in your app. Additionally, you should try to minimize the use of state in your app and instead try to use props as much as possible. This is because props are considered to be immutable, meaning they cannot be changed within a component. This can make your app easier to reason about and debug, as the flow of data is more predictable.

Remember, when using state in your React app, it's important to only modify it using the setState method and to try to minimize its use in favor of props whenever possible. This can help you write more predictable and maintainable code.

If you have a larger, more complex app, you may want to consider using the useState hook or a state management library like Redux to help manage your app's state. These approaches can help you scale your app and make it easier to manage as it grows.

We hope this tutorial has been helpful in getting you started with managing state in your React app. If you have any questions or would like to learn more, there are many resources available online that can provide further guidance and information on this topic.

There are also a few other ways to manage the state in a React app, such as using the useState hook or using a state management library like Redux. These approaches can be useful in larger, more complex apps, but for smaller apps, using props and state as we have shown here is usually sufficient.

That's it for this tutorial on managing state in a React app! We hope you now have a better understanding of what state and props are, how they are used in a React app, and how to manage them effectively.

Want us to build yours instead?

On a project, every milestone ends with a link to the working build, and the code, the accounts and every password are yours at the handover.

What we’d build for you is on Services.

Send us the brief, or start with the free call.

Page updated .

Privacy preferences

Choose whether this browser sends visit data to our analytics tools. The site works the same either way.

Google Analytics, Amplitude and Microsoft Clarity show us which pages get read. Off means none of the three loads.

Always on

Sentry error reports, with personal data removed, and the Cloudflare spam check on the brief form.

Each cookie is listed in our Cookie Policy.