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

191
Visualizações
Getting initial useState list values when returning from a sub componet to save data instead of current state

I have recreated my problem in a simplified code example. Follow these steps to reproduce it:

  1. Click on one of the three list items to edit the value in the off-canvas box.
  2. Change the value and click the save button.
  3. Select a different item to edit & save.
  4. Note: the original edited item has reverted back to its initial state. I have the console.logging in the save method to show that the list(state) is not the current (visible) version but the initial state.

Sandbox code example

I have an inelegant solution(workaround) that I will put as an answer but it doesn't explain what or why this is happening. I have 3 off-canvas editors like this on my page & one does work as expected but the other two loose state when calling their save functions in the parent.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Here is my bad (ugly) workaround:

  1. Pass the state object (myList) to the child in props object.

App.js

  function open(e, item, itemIndex) {
    setPropArgs({ ...propArgs, editThis: item, index: itemIndex, show: true, itemList: myList });
  }
  1. Return the list back to the parent in save method call & use the passed variable instead of state.

MyEditor.js

    <Button type="button" onClick={(e) => props.save(edit, props.index, props.itemList)}>
      Save
    </Button>

App.js

  function save(edit, index, itemList) {
    // console.log(myList);
    const newList = [...itemList];
    newList[index] = edit;
    setMyList(newList);
    setPropArgs({ ...propArgs, show: false });
  }

This is bad because MyEditor shouldn't need to know anything about the parent, it doesn't read or edit the list at all & there shouldn't be a need to pass around a copy of the state that could become out of date (if I wasn't blocking with the canvas).

about 4 years ago · Juan Pablo Isaza Relatório

0

Okay... I found the answer that I was looking for. Thanks to Marco Nisi for this Answer to a very similar question that has not got much attention

My forked code solution here

The solution is to move the callback (save function) into be created when the canvas is shown instead of when the functional component is initialized creating a fresher clone of the state. I can still see this as being problematic if you have an example where the state is being updated in the background or if the off-canvas is not blocking other edits. Note: You don't need the callback defined in the open function but it does need to be added(replaced) to the props object there.

  function open(e, item, itemIndex) {
    save = (edit, index) => {
      console.log(myList);
      const newList = [...myList];
      newList[index] = edit;
      setMyList(newList);
      setPropArgs({ ...propArgs, show: false });
    };
    setPropArgs({
      ...propArgs,
      editThis: item,
      index: itemIndex,
      show: true,
      save: save
    });
  }
about 4 years ago · Juan Pablo Isaza 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