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

184
Visualizações
How can a React useState setter update this state variable without using the state variable?

Can someone explain why this code works?

import { useState } from 'react';

export default function RandomList() {
    const [numbers, setNumbers] = useState([]);

    const addNumber = () => {    
        setNumbers(whatever => {
            return [...whatever, Math.random()];
        });
    };

    return (
        <div>
            <h1>Random Numbers</h1>
            <button onClick={addNumber}>Add a Number</button>
            <ul>
                {numbers.map((number, index) => (
                    <li key={index}>{number}</li>
                ))}
            </ul>
        </div>
    );
}

Namely:

(1) Where is whatever being stored, is it in a closure inside the function? Or is it merely a temporary parameter which is used to update the internal state of numbers?

(2) Is there any advantage in sending a function into the useState setter as in the above code, rather than doing this:

numbers.push(Math.random());
setNumbers([...numbers]);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Where is whatever being stored,

whatever is the argument to the function you've created. React will call your function, passing in the current value of the state as that argument, and then you return what you want the new state to be.

Is there any advantage in sending a function into the useState setter as in the above code, rather than doing this:

The function version of set state guarantees that you're using the latest value of the state, and so it makes a category of bugs related to stale closure variables impossible.

For example, if you try to set state twice in a row without using the function, the second set state will overwrite the first and so you only get one new number:

setNumbers([...numbers, Math.random()]);
setNumbers([...numbers, Math.random()]);

But if you use the function version instead, it will keep both changes:

setNumbers(prev => [...prev, Math.random()]);
setNumbers(prev => [...prev, Math.random()]);

Of course, you probably won't write two lines of code like that right next to eachother, but in complicated components you can get similar behavior, just with the lines of code not so obviously related.

about 4 years ago · Juan Pablo Isaza Relatório

0

Is there any advantage in sending a function into the useState setter as in the above code, rather than doing this:

When setNumbers([...numbers]); updates the state, the changes are not reflected immediately in the count variable. Rather React schedules a state update, and during the next rendering in the statement const [numbers, setNumbers] = useState([]); the hook assigns to numbers the new state value.

so to avoid this asynchronous nature and we pass callback to setNumber();

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