Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!