Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda