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

209
Vistas
OBSOLETE: React: setState for primitive value doesn't work with ===

EDIT: Obsolete, I made some mistake in another piece of code and the received data had the wrong data type.

I have a variable that stores the index of a selected item. I used a conditional expression based on that variable to add/remove a class name so said item is rendered in a distinguishing way. I boiled down the problem to this snippet:

function App() {
  const [selectedItem, setSelectedItem] = setState(-1);

  setSelectedItem(0);
  console.log(selectedItem);

  return (
    <>
    {selectedItem !== 0 && <p>no item selected</p>}
    {selectedItem === 0 && <p>item {selectedItem} selected</p>}
    </>
  );
}

This snippet always displays that no item is selected.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The hook is called useState, not setState.

Calling setSelectedItem inside the render function will trigger an infinite loop. You need to move it to an effect hook so it only runs one.

export default function App() {
  const [selectedItem, setSelectedItem] = useState(-1);

  useEffect(() => {
    setSelectedItem(0);
  }, []);
  
  console.log(selectedItem);

  return (
    <>
    {selectedItem !== 0 && <p>no item selected</p>}
    {selectedItem === 0 && <p>item {selectedItem} selected</p>}
    </>
  );
}
about 4 years ago · Santiago Trujillo Denunciar

0

What is setState ? Do you mean useState ? And also, you shouldn't update the state like this, you should do it in a useEffect, and use an empty array as dependency, so it will run only once, when your component is being mounted:

useEffect(() => {
    setSelectedItem(0);
},[])

Then it is working like a charm

about 4 years ago · Santiago Trujillo Denunciar

0

Replace === and !== with == and !=, respectively, and voila, it works. Alas, a warning is reported that one shall use === instead.

selectedItem is an array, apparently primitives make bad references. Still, it is bizarre to some extent that inside <p> the variable is unboxed automatically, while for evaluating === it isn't and thus an array is compared to a primitive, which is not true, no matter the values. == compares every shit in JS that you feed it with, so here it works.

Hope, this saves somebody 2 hours of debugging.

If somebody has a correct workaround for this, please share below.

about 4 years ago · Santiago Trujillo 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