What is state

In React, state refers to a built-in object that holds data or information about the component. It is used to create dynamic and interactive components by enabling React to keep track of the changes in the component’s data over time. When the state of a component changes, React re-renders the component to reflect the new state, ensuring the UI is always up to date.

Key Characteristics of State:

Mutable: state is mutable and can be changed by the component itself while props, which are read-only.

Private: State is local to the component where it is defined and cannot be accessed or modified directly by other components.

Triggers Re-render: When state changes, React automatically re-renders the component to reflect the updated state.

Managing State in Functional Components:

With the introduction of React Hooks in version 16.8, functional components can now use state through the useState hook.


import React, { useState } from 'react';

function Counter() {
  // Declare a state variable named "count" with an initial value of 0
  const [count, setCount] = useState(0);

  // Function to increment the count
  const increment = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={increment}>Click me</button>
    </div>
  );
}
export default Counter;

Explanation:

useState Hook:

The useState hook is used to declare a state variable. It takes an initial state value as an argument and returns an array with two elements:

  1. The current state value (count).
  2. A function to update the state (setCount).

State Initialization:

const [count, setCount] = useState(0); initializes the state variable count with an initial value of 0.

Updating State:

The increment function updates the state by calling setCount(count + 1). This triggers a re-render of the component with the new state value.

Rendering the Component:

The component renders a div containing a paragraph that displays the current count and a button that increments the count when clicked.

Managing State in Class Components:

Before hooks, class components were the primary way to manage state in React. State is managed through the this.state object and updated using the this.setState method.


import React, { Component } from 'react';

class Counter extends Component {
  constructor() {
    super();
    // Initialize state
    this.state = { count: 0 };

  }

  // Method to update state
  increment() {
    this.setState({count:this.state.count+1})
  }

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

export default Counter;

Explanation:

State Initialization:

State is initialized in the constructor using this.state = { count: 0 };.

Updating State:

The increment method updates the state using this.setState, which triggers a re-render.

Rendering the Component:

The render method returns JSX that renders the current state and a button to update the state.