useState React Hook Explained With Example

Empower Functional Components with Dynamic State using useState Hook

The useState hook is one of the core React Hooks that allows you to add state to functional components. With useState, you can introduce dynamic data and interactivity into your components without the need for class components. This hook is used for managing local component state and is particularly useful for handling simple state changes and user interactions. Let’s explore the useState hook in more detail and cover some advanced techniques.

react-hooks-useState_3048437105270441936.png

Syntax:

const [state, setState] = useState(initialState);
  • state: The current value of the state variable.
  • setState: A function used to update the state variable.
  • initialState: The initial value of the state.

Example 1: Counting with useState:

import React, { useState } from 'react';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};

In this example, the useState hook is used to create a state variable named count with an initial value of 0. The setCount function allows you to update the value of count when the button is clicked, triggering a re-render of the component.

Example 2: Managing Input Fields:

import React, { useState } from 'react';

const InputForm = () => {
  const [text, setText] = useState('');

  const handleInputChange = (event) => {
    setText(event.target.value);
  };

  return (
    <div>
      <input
        type="text"
        value={text}
        onChange={handleInputChange}
        placeholder="Enter text"
      />
      <p>Entered text: {text}</p>
    </div>
  );
};

In this example, the useState hook is used to manage the value of an input field. The text state variable stores the input’s value, and the setText function is used to update it when the user types into the input field.

Advanced Techniques:

1. Functional Updates:

import React, { useState } from 'react';

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

  const increment = () => {
    setCount((prevCount) => prevCount + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
};

Using a functional update inside setCount can help avoid potential race conditions and ensure the update is based on the previous state.

2. Initializing State Based on Props:

import React, { useState, useEffect } from 'react';

const UserProfile = ({ initialName }) => {
  const [name, setName] = useState(initialName);

  useEffect(() => {
    setName(initialName);
  }, [initialName]);

  return <div>User's Name: {name}</div>;
};

You can initialize state based on props using useState and synchronize it with the useEffect hook.

3. Multiple State Variables:

import React, { useState } from 'react';

const Form = () => {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
  // ... other state variables

  const handleSubmit = (event) => {
    event.preventDefault();
    // Process form data
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
        placeholder="Name"
      />
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Email"
      />
      {/* ... other form fields */}
      <button type="submit">Submit</button>
    </form>
  );
};

You can use multiple instances of useState to manage different state variables in a single component.

The useState hook provides a simple and effective way to introduce and manage state within functional components. By understanding its syntax and applying advanced techniques, you can create interactive and dynamic user interfaces in your React applications.

Related Posts