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

101
Visualizações
Setting the default value of state variable as output of a function

I have a state variable:

const [rowValues, setRowValues] = React.useState(() => {
            setAllValues(false);
        });

I have attempted to initialize the default value in the Lazy Initialization Fashion In other words, i want the default value of the state to be the return value of setAllValues(false).

Now here is the setAllValues function:

async function setAllValues(value) {
        await rows.forEach((element) => {
            temp = { ...temp, [element.id]: value };
        });
        console.log('| temp', temp);
        return temp;
    } 

rows is a variable destructured from the props. Initial renders, the value is [], next times it renders as the expected value. Look at the image to better under-stand what im saying here:
enter image description here

Since the initial value of rows is [], the state gets the wrong default value. I want the function (setAllValues(false)) to run when the value of rows is [{...},{...},{...}..] , or how do i prevent rows from having the value []? Please Help.

about 4 years ago · Juan Pablo Isaza
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