DEV Community

Golam_Mostafa
Golam_Mostafa

Posted on • Edited on

2 1

useState when to use?

1. Managing simple state:

import React, { useState } from 'react';

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

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

2. Managing boolean state:

import React, { useState } from 'react';

function Example() {
  const [isOn, setIsOn] = useState(false);

  return (
    <div>
      <p>The light is {isOn ? 'on' : 'off'}</p>
      <button onClick={() => setIsOn(!isOn)}>
        {isOn ? 'Turn off' : 'Turn on'}
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

3. Managing complex state:

import React, { useState } from 'react';

function Example() {
  const [person, setPerson] = useState({ name: '', age: 0 });

  const handleInputChange = event => {
    const { name, value } = event.target;
    setPerson({ ...person, [name]: value });
  };

  return (
    <div>
      <p>Name: {person.name}</p>
      <p>Age: {person.age}</p>
      <input
        type="text"
        name="name"
        value={person.name}
        onChange={handleInputChange}
      />
      <input
        type="number"
        name="age"
        value={person.age}
        onChange={handleInputChange}
      />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

4. Managing array state:

import React, { useState } from 'react';

function Example() {
  const [todos, setTodos] = useState([]);

  const handleAddTodo = () => {
    setTodos([...todos, { id: Date.now(), text: 'New todo' }]);
  };

  return (
    <div>
      <ul>
        {todos.map(todo => (
          <li key={todo.id}>{todo.text}</li>
        ))}
      </ul>
      <button onClick={handleAddTodo}>Add todo</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

5. Updating state based on previous state:

import React, { useState } from 'react';

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

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

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={handleIncrement}>Click me</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

These are just a few examples of the many use cases of the useState hook. The hook is very versatile and can be used to manage any kind of state within a React component.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

Retry later