State in React

What a React component looks like is a reflection of its state - in this lesson, we'll look at how to use it and some potfalls

What is state?

The core of every React component is its state. The state determines what the component looks like, and you can update that as you go. The entire page won’t reload if a component’s state is updated only the component will - hence, it allows you to create pages that are dynamic and interactive. You can think of a component’s state like water’s temperature; changing temperature can significantly change what water looks like. If the temperature is brought below 0°C the water will solidify, if it is between 0°C and 100°C it will be a liquid, and if brought above 100°C it will turn into a gas and evaporate. You can change how water behaves and looks just by changing the value of temperature and that is exactly what state is in React, you can change how components behave and look just by changing the state.

Creating a component with state

To be a little less abstract, the state is just an object in a React component that you can save and get data from such as strings and numbers. Let’s see how the state is defined in a React component.

Get hands-on with 1200+ tech skills courses.