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

266
Vistas
How do array equals useState declarations work in Javascript?

In functional components within ReactJS, the following line allows the creation of:

  1. A variable to record the current count
  2. A method to save the state when called

Example in JS:

let [count, setCount] = useState([]);

How does this work from a declaration point of view and what is this kind of declaration called?

I understand basic declarations like let array = []; etc but am not familiar with the other way around.

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

0

When you are using useState in reactJS. It will return an array of two values

  1. A variable to record the current count.
  2. A method to save the state when called.

So you can either get the value individually using index as:

let arr = useState([]);

const count = arr[0];
const setCount = arr[1];

or you can also use destructuring as:

let [count, setCount] = useState([]);

Below is example of how destructure works

function customUseState(initialValue) {
  let val = initialValue;
  function changeVal(newVal) {
    val = newVal
  }
  return [val, changeVal];
}

// This is how destructure works
const [count, setCount] = customUseState(0);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Usestate returns an array with two elements first is the value or state in your case "count" and second is function used to update the state in your case "setCount". Here you are not declaring them you are destructuring them. Its same like we do object destructuring.

{name,age}={obj} we are destructuring name and age from the obj same way you are extracting count and setCount from usestate. And array destructuing depends on the position of elements rather than variable names whereas in object destructuring it depends on variable names.

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