Understanding How State Works
In this tutorial, we explore the concept of state in React and how it allows components to be dynamic and self-aware. This lesson is foundational for React developers aiming to build interactive user interfaces.
1. Props vs. State
- Props: Passed from parent to child, immutable by the receiving component.
- State: Declared within a component, mutable, and triggers re-renders when updated.
2. Declaring State
State is initialized in the constructor of a React component:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
clicks: 0
};
}
}
- The
constructoris where we define the initial state. - React will track state changes and automatically re-render the component when state is updated.
3. Updating State with setState
React provides the setState method to safely update state and trigger re-renders:
handleClick = () => {
this.setState((prevState) => ({
clicks: prevState.clicks + 1
}));
};
- Never mutate state directly (e.g.,
this.state.clicks++is incorrect). - Use
setStateto ensure React knows a change has occurred.
4. Binding State Updates to Events
In this example, a button click updates the state and reflects the change in the UI:
render() {
return (
<button onClick={this.handleClick}>
{this.state.clicks}
</button>
);
}
5. React’s Re-Rendering Behavior
- When
setStateis called, React automatically calls therendermethod. - Only the updated parts of the DOM are re-rendered, making React efficient.
Key Takeaways
- State Tracks Changes: State allows components to be interactive and self-updating.
- Use
setState: Always usesetStateto update state safely and trigger React’s lifecycle methods. - Dynamic UI Updates: Bind state updates to events for dynamic, user-driven UI changes.
This tutorial introduced the basics of state in React. In the next session, we’ll build on this knowledge to create state-driven animations for interactive user interfaces.
Video Transcript
00:00So far, every time we wanted
00:02to change something in a component
00:04or make it dynamic, we've used properties or props.
00:08Now props are perfect in a scenario
00:10where the component itself doesn't need to be aware
00:14of a change that it might have.
00:16Well, in a situation where you want your component to listen
00:20or to be aware of changes that are happening within itself,
00:23those type of changes are called state.
00:26And in this lecture we're gonna take a look
00:27and understand how states work, how state works.
00:31And by the end of this lecture, you're gonna get
00:33and know really the core competencies that you need to know
00:36to become a React developer.
00:40As you might have assumed, components sometimes change.
00:44While technically speaking,
00:45whenever we send in a property,
00:47we never change those properties
00:49unless there's a change happening in a parent.
00:52And then it could change properties
00:54that are sent to its children.
00:55But a component itself will never change on its own.
00:59A property, the way changes could be done inside
01:02of a component in React is using something
01:05that is called state.
01:07Now, the state itself is something
01:09that you basically declare in your constructor.
01:11Literally telling the world
01:12or telling react, expect this to change.
01:15And whenever this changes,
01:17then we're gonna need to re-render.
01:19Now in the case of state, I wanna create the state variable.
01:23It's a this variable and define a name.
01:26In this case, I'm gonna go ahead and set clicks to be zero
01:28because what I really wanna do is every time a click
01:31happens, I want to elevate that click to a number above.
01:36Now, the way I'm gonna do that is I'm not gonna approach
01:39directly that object Again,
01:41I only approach the object directly in the constructor
01:43when I'm creating it.
01:45Every other time after that, I'm gonna have to call the this
01:48that set state.
01:49Now the reason is very simple.
01:51If I would change the object directly, react would not know
01:54that a change has happened.
01:56And as we want to use these,
01:58you could put it in many properties as you want,
02:00but as we want react to know about it
02:02and then called the re rendering process.
02:05Again, we definitely wanna call the set state
02:07because we should not and are not allowed
02:09to change things directly ourself that is in the control
02:13of the React library itself or react dom for that matter.
02:16So we're basically in our scenario,
02:19so we're basically declaring to react that something changed
02:22and you should call the render method again.
02:24Now I'm gonna go ahead and I'm just gonna call this state.
02:27Do click to fetch out of it,
02:30the current click state and add one to it.
02:32I'm never gonna change the click directly,
02:34but instead of that I'm basically going
02:35through the set state and it's going to change it for us.
02:39Now lemme just go ahead here
02:41and I'm just gonna, um, output out that state click.
02:43So we could see that that number is gonna elevate
02:46each time we click on it.
02:47If I go ahead here
02:49and I just click here, we'll see
02:50that our number is gonna go up each time.
02:52Now let's just go ahead
02:53and also create a visual change in our application.
02:56So I'm gonna go ahead and literally take this,
02:59This state click and I'm gonna cut this out
03:02and I'm just gonna send it into our big button down here.
03:05So inside of our label, instead of actually having a label,
03:08I'm just gonna go ahead here
03:09and send in our, this state click.
03:12So in other words, every time our element is gonna be
03:16clicked on, initially it's gonna have nothing inside.
03:18But once I click on it,
03:19our numbers will start growing 5, 6, 7, 8,
03:23and so on and so forth.
03:24So beautiful. So our react component is updating each time
03:28and that's exactly how you create those state changes.
03:33In this lecture. We were introduced to state
03:36and soar how they work.
03:37And we're ready now to start animating, which is exactly
03:40what we're gonna do in the next lecture.
