Course Content
Run Your First Project
This course is designed to help you kickstart your journey in project management. Whether you're a beginner or looking to brush up on your skills, this course will provide you with the essential knowledge and tools needed to successfully manage your first project from start to finish. Join us in "Run Your First Project" and gain the confidence and skills to manage your next project with success!
0/2
Rendering Elements
In React, elements are the smallest building blocks of React applications. React elements are immutable and describe what you want to see on the screen. They are used to construct the user interface by defining how it should look at a given point in time. When rendering, React elements are passed to the React DOM, which efficiently updates and renders the elements to the actual DOM.
0/12
Build Your Application
0/1
Learning Journey Of React
About Lesson

React state is a key concept for managing data within a component. It allows you to create interactive and dynamic user interfaces by keeping track of the changes in data over time.

Here’s an in-depth look at React state:

State in Class Components

In class components, state is a built-in object that stores property values that belong to the component. When the state object changes, the component re-renders.

Defining State

State is defined in the constructor of a class component:

import React from "react";

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }
  
  render() {
    return <h2>It is {this.state.date.toLocaleTimeString()}.</h2>;
  }
}

Updating State

State is updated using the setState method. This method ensures that the component re-renders with the new state values.

We can set up a scheduled task to refresh the state at regular intervals, in order to achieve the purpose of re-rendering.

App.js:

import React from "react";

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

  componentDidMount(){
    this.IntervalId = setInterval( () => { this.tick() }, 1000 );
  }

  componentWillUnmount(){
    clearInterval( this.IntervalId );
  }
  
  tick() {
    this.setState({ date: new Date() });
  }
  
  render() {
    return <h2>It is {this.state.date.toLocaleTimeString()}.</h2>;
  }
}
export default function App() {
    return (
      <Clock></Clock>
    );
}

.

The methods componentDidMount() and componentWillUnmount() are called lifecycle hooks.
The componentDidMount() hook is executed after the component has been output to the DOM, and we can set a timer in this hook.
The IntervalId, stored in this.IntervalId, is the ID of the timer, and we can clear the timer in the componentWillUnmount() hook.

.

State in Functional Components (Hooks)

With the introduction of hooks in React 16.8, functional components can also have state. The useState hook is used for this purpose.

Defining State with useState

The useState hook returns an array with two elements: the current state value and a function to update it.

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

export default function App() {
    return (
      <Counter></Counter>
    );
}

.

You update the state by calling the state updater function (in this case, setCount) with the new state value.

You can set an initial state value directly in the constructor for class components or within the useState call for functional components.

const [count, setCount] = useState(0);  // Functional Component
this.state = { count: 0 };  // Class Component

Note: always use setState or the state updater function from useState to update state, never mutate state directly.

By effectively managing state, you can build complex and interactive UIs that respond to user inputs and data changes efficiently.