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

258
Views
I iterate over an array with map and then try to change a boolen of an object from false to true with an onClick. UI doesn't reflect it but log does

CodeSandbox - https://codesandbox.io/s/distracted-taussig-n7e7q3?file=/src/App.js

As you can see, I iterate over the flaggers array with .map and render <div>true</div> or <div onClick={() => setToTrue(flag)}>false</div>. I assumed that if I were to click the second div, the refer property of that flag would be set to true and the component would re-render, making the div change to <div>true</div> but that doesn't seem to be the case.

In the setToTrue function I console.log the post object, and I can see that the refer property of the second flag has changed to true, but it is not shown in the UI.

import "./styles.css";

export default function App() {
  const post = {
    flaggers: [
      {
        refer: false
      },
      {
        refer: false
      }
    ]
  }

  const setToTrue = (flag) => {
    flag.refer = true;
    console.log(post)
  }

  return (
    <div className="App">
      {post.flaggers.map((flag) => (
        <div>
          {flag.refer ? <div>true</div> : <div onClick={() => setToTrue(flag)}>false</div>}
        </div>
      ))}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, that's not how react you have to setState value to trigger the component to rerender which will cause to UI change try the below code it will work fine as I set a value on onClick that causes the component to rerender in short. I would suggest reading the documentation before going into the coding and know-how react works

React Documentation

export default function App() {
  const [post, setPost] = React.useState([
    {
      refer: false,
    },
    {
      refer: false,
    },
  ]);

  const setToTrue = (boolFlag, index) => {
    const tempPost = [...post];
    tempPost[index].refer = boolFlag;
    setPost(tempPost);
  };

  return (
    <div className='App'>
      {post.map((flag, index) => (
        <div key={`${index}flag`}>
          {flag.refer ? <div onClick={() => setToTrue(false, index)}>true</div> : <div onClick={() => setToTrue(true, index)}>false</div>}
        </div>
      ))}
    </div>
  );
}

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