Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to update state in ReactJS

Introduction to ReactJS

ReactJS is a JavaScript library developed by Facebook. It's used for building user interfaces, specifically for single-page applications. You get to build reusable UI components, which is a significant advantage for maintaining your code when working on larger scale projects.

However, before we dive into the topic, it's important to understand the basic terminology. In programming, 'state' refers to the condition or quality of something at a specific moment in time. In ReactJS, 'state' is similar to 'props', but it is private and can be controlled by the component itself.

Understanding State in ReactJS

Imagine state as the heart of your components in React. It's the data that determines how that component renders and behaves. In other words, it's the "memory" of your component.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { name: 'John' };
  }
}

In this code snippet, we've created a state object with a property called 'name' that has a value of 'John'. This state is exclusive to the 'App' component and it determines how this component will behave and render.

Updating State in ReactJS

In React, state is read-only. This means you cannot directly modify it. Instead, you have to use the 'setState()' method. This method schedules an update to the component's state object and tells React that this component and its children need to be re-rendered with the updated state.

Here's an example:

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { name: 'John' };
    this.changeName = this.changeName.bind(this);
  }

  changeName() {
    this.setState({ name: 'Jane' });
  }
}

In this code snippet, we've added a method called 'changeName'. This method updates the 'name' property of the state object to 'Jane' when it's called.

The Importance of setState()

Remember the heart analogy? Your heart can't just decide to change its rhythm on its own—it needs signals from your brain. Similarly, your components can't just change their state on their own—they need signals, or calls to the 'setState()' method.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
    this.increaseCount = this.increaseCount.bind(this);
  }

  increaseCount() {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={this.increaseCount}>
          Click me
        </button>
      </div>
    );
  }
}

In this code, we've created a counter where the state object has a 'count' property initially set to 0. When you click the button, the 'increaseCount' method is called, which increments the 'count' property in the state object by 1. This updated state then re-renders the component.

Conclusion

Understanding and managing state is a fundamental part of using ReactJS. It's like the pulses your brain sends to keep your heart beating—it keeps your components rendering and behaving correctly. Remember, you can't change state directly. Instead, use the 'setState()' method to schedule updates to a component's state.

Happy coding, and remember to keep your components' hearts healthy and their states updated!