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

211
Vistas
React : why are my components not re-rendering when controlled by an array state?

I have a bunch of buttons in my app and I change their className when they are clicked ("myButton" or "activeMyButton"). Whenever I click on a non-active button, it's not re-rendering. But when I click on an active button, it's re-rendering (and if I clicked on non-active buttons before, they're re-rendering with this one). It doesn't make any sense to me. Here's my code : the button component

export default function MyButton({
  buttonFunction,
  buttonParameter,
  activeButton,
}) {
  return (
    <button
      className={activeButton ? "myButton activeMyButton" : "myButton"}
      onClick={() => {
        buttonFunction(buttonParameter);
      }}
    >
      {buttonParameter}
    </button>
  );
}

And here's the parent component :

export default function Mathoperators() {
  const [activeOperators, setActiveOperators] = useState(["+"]);

  const handlingOperators = (operatorInput) => {
    let stockOperators = activeOperators;
    if (
      stockOperators.length > 0 &&
      stockOperators.some((operator) => {
        return operator === operatorInput;
      })
    ) {
      stockOperators = stockOperators.filter((operator) => {
        return operator !== operatorInput;
      });
      setActiveOperators(stockOperators);
    } else {
      stockOperators.push(operatorInput);
      setActiveOperators(stockOperators);
    }
  };

  const handlingOperatorButtonClass = (operatorInput) => {
    return activeOperators.some((operator) => {
      return operator === operatorInput;
    });
  };

  return (
    <section className="optionsContainer">
      <MyButton
        buttonParameter={!mathPlus}
        buttonFunction={setMathPlus}
        activeButton={mathPlus}
      />
      <MyButton
        buttonParameter="−"
        buttonFunction={handlingOperators}
        activeButton={handlingOperatorButtonClass("−")}
      />
      <MyButton
        buttonParameter="×"
        buttonFunction={handlingOperators}
        activeButton={handlingOperatorButtonClass("×")}
      />
      <MyButton
        buttonParameter="÷"
        buttonFunction={handlingOperators}
        activeButton={handlingOperatorButtonClass("÷")}
      />
    </section>
  );
}

If I switch from this array state to four boolean states, everything is working fine. But I'd like to know if I'm doing something wrong with this version. Thanks !

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When you write let stockOperators = activeOperators you're not creating a new array, you're just creating a variable that points to the activeOperators state. Thus stockOperators.push(operatorInput) actually mutates your state directly and that creates this weird behavior that you're seeing.

let stockOperators = [...activeOperators] should fix it!

about 4 years ago · Juan Pablo Isaza Denunciar

0

change it to let stockOperators = [...activeOperators];

this happened because when you do let stockOperators = activeOperators;

it will copy the reference to the value and change it mean you change the start itself so the when you put it in the setState it will looks like its never been changed

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