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

259
Vistas
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 Respuestas
Responde la pregunta

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