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

162
Visualizações
useEffect wont run when state gets changed

Why doesn't useEffect run when sorting gets changed? I only want it to run when sorting.options get adjusted, but for some reason, when I add options to sorting.options the useEffect won't run happen; however, when I only have "sorting" it works perfectly even though only options get changed in handleChange? I dont want to have only sorting in there because I don't want the useEffect to run when sorting.value gets changed, for example.

const handleChange=index=>event=>{
    let newOptions = sorting.options
    newOptions[index]["value"]=event.target.value
    setSorting((prevState)=>({
      ...prevState,
      options:newOptions
    }))
    console.log("sorting changed")
  }
  useEffect(()=>{
    console.log("Change happend")
  },[sorting.options])//This works perfectly fine when only sorting is there, but when i put in sorting.options nothing happens
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

As Quentin said, try

let newOptions = sorting.options.slice(0)

To copy the options array instead of mutating it

about 4 years ago · Santiago Gelvez Relatório

0

The problem is that you keep the reference of the original object throughout different render. Just replace by a copy

import React, { useState, useEffect } from "react";

export default function Home() {
  const [sorting, setSorting] = useState({
    options: { 1: { value: 2 } },
    email: "vapa@dieheli.us",
  });

  const handleChange = (index) => (event) => {
    let newOptions = sorting.options; // Here you are getting the reference of sorting.opions which is constant between renders
    // let newOptions = { ...sorting.options }; // Just replace previous line for a copy. 
    newOptions[index]["value"] = event.target.value; // Then you mutated the same object
    setSorting((prevState) => ({
      // setSorting creates a new object reference
      ...prevState,
      options: newOptions, // but options references never changed
    }));
  };

  useEffect(() => {
    console.log("Change happend", sorting);
  }, [sorting.options]);

  return (
    <div>
      Home <input onChange={handleChange(1)} />
    </div>
  );
}
about 4 years ago · Santiago Gelvez 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