Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
Any method to use single useState()

What I wanted to know ,that is there any method / technique to replace many useState() hook in one hook.

Like,

const [firstName, setFirstName] = useState('');
const [email, setEmail] = useState('');
const [age, setAge] = useState('');
const [people, setPeople] = useState([]);

Instead of writing the whole thing again and again , we define it any one of them.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use the useReducer hook instead of useState.

This is their official docs on useReducer

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create a state object and use it.

Note: When you set state, make sure you merge with the previous state. don't just call setState("abc");

const [state, setState] = useState({ firstName: "", email: "" });

// somewhere in your code, This is important
setState((pre) => ({ ...pre, firstName: "John", email: "ab@gm.co" }));
about 4 years ago · Juan Pablo Isaza Denunciar

0

NO there isn't any way to define all that state using a single useState. unless you want to manage your state as a single object which you can define like this

const [state, setState] = useState({
    // key: value
});

There are advantage in keeping each state separate from other state. or you can simple use a class component instead of functional component where you can have all your state manage inside of a single object

By the way this bring some kind of head-hake for each state property update you have to rely on spread operator to avoid to replace the entire object with a single value instead of an object with properties

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda