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

266
Visualizações
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 Respostas
Responde à pergunta

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