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

102
Vistas
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 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