Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set the intial state to a data in ReactJS

Introduction

If you're reading this, I assume you're already familiar with the concept of ReactJS. If not, don't worry! ReactJS is essentially a JavaScript library that is widely used to build user interfaces, particularly for single-page applications. And yes, I did say library, not framework, which means it's a collection of pre-written code that developers use to solve common tasks.

Now, if you've dipped your toes into ReactJS, you've likely come across something called 'state'. The state in ReactJS can be a bit tricky to understand, especially if you're new to programming. But don't be scared! In this blog post, we'll delve into how to set the initial state to a data in ReactJS.

What is State?

Firstly, let's discuss what 'state' is. Imagine you're building a digital clock. The time that is displayed on the clock is constantly changing. In the context of ReactJS, this time would be considered the 'state' of the clock component. Essentially, the state is the data that changes over time. It's similar to how your own 'state' changes throughout the day; you might feel happy in the morning, tired in the afternoon, and relaxed in the evening.

React components can have their own local state, and this state can be initialized and manipulated within the component itself.

Initializing State

Now that we understand what state is, let's discuss how to initialize it. In React, we typically initialize the state in the constructor method of a class component, or by using a state variable in a functional component.

Let's look at an example with a class component:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {time: new Date()};
  }
  //...
}

In this example, we've created a state object in the constructor of our Clock component. The state object has a property called time, which we've set to the current date and time using JavaScript's new Date().

Now, let's set the initial state in a functional component:

function Clock() {
  const [time, setTime] = React.useState(new Date());
  //...
}

In this case, we're using the useState hook provided by React to create a state variable time and a corresponding setter function setTime. We've initialized time to the current date and time, just like in the class component.

Updating State

You might be wondering, "Great, we have our state, but how do we change it?" Remember our clock example? The time doesn't stay the same—it changes every second. We need a way to update the state of our component.

In React, we use the setState method in a class component or the setter function from useState in a functional component to update the state. Here's how you can do it:

For a class component:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {time: new Date()};
  }

  tick() {
    this.setState({
      time: new Date()
    });
  }
  //...
}

In this example, we've added a tick method to our Clock component. This method updates the time state to the current date and time.

For a functional component:

function Clock() {
  const [time, setTime] = React.useState(new Date());

  function tick() {
    setTime(new Date());
  }
  //...
}

In the functional component, we're using the setTime function that we got from useState to update the time state.

Conclusion

I hope that by now, you've got a solid understanding of what state is in React and how to set the initial state. Just remember, the state is like the heart of your component—it's what gives your component life and allows it to interact dynamically with the user.

As with any technology or programming concept, the best way to understand and master it is through practice. So, I encourage you to create your own React components, initialize their state, and see what happens when you update it. Happy coding!