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

122
Visualizações
Parent component does not rerender after updating its state through child component

I checked some of the threads about this and tried to fix it, but no succes yet.

I have a parent and a child component. In the parent component, I declare a state and pass the function to update this state on to my child component.

  function ProfileGallery() {
  const [selectedPet, setPet] = useState(null);
  const [filterText, setFilterText] = useState('');
  const [pets, setPets] = useState([]);
  const [componentState, setState] = useState('test');

  const updateState = (state) => {
        setState(...state);
      };

  return (
       <PetInformation
                selectedPet={selectedPet}
                componentState={componentState}
                triggerParentUpdate={updateState}
              />
);
}

In my child component, I do the following:

function PetInformation({ componentState, triggerParentUpdate, ...props }) {
  const [status, setStatus] = useState('Delete succesful');
  const { selectedPet } = { ...props } || {};
  const updatedComponentState = 'newState';

  useEffect(() => {
    if (status === 'Delete pending') {
      deletePet(selectedPet).then(() => setStatus('Delete succesful'));
    }
  }, [status]);

  const handleDelete = () => {
    setStatus('Delete pending');
  };

return (
    <button
            className="btn btn-primary pull-right"
            onClick={() => {
              handleDelete();
              triggerParentUpdate(updatedComponentState);
            }}
            type="button"
          >
            Delete Pet
    </button>

There's of course more code in between, but this shows the gist of what I'm trying to achieve. I want to delete an item I selected in my gallery and have that delete reflected once I click the delete button -> my ProfileGallery needs to re-render. I'm trying to do this by passing that function to update my state on to the child component. I know JS won't consider state changed if the reference remains the same, so that's why I am passing a new const updatedComponentState on, since that should have a different reference from my original componentState.

Everything works, the item gets removed but I still need to manually refresh my app before it gets reflected in the list in my gallery. Why won't ReactJS re-render my ProfileGallery component here? Isn't my state getting updated?

about 4 years ago · Juan Pablo Isaza
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