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

161
Visualizações
Using ImmerJS to update child (by id) within a parent (by id)

How do I update the content of child12 using immerJS

const [fields, setFields] = useState([
    {
        name: "Parent 1",
        id: "parent1",
        children: [
            {id:"child11", content=""},
            {id:"child12", content=""}
        ],
    },
    {
        name: "Parent 2",
        id: "parent2",
        children: [
            {id:"child21", content=""},
            {id:"child22", content=""}
        ],
    },
]);

I have been able to achieve changing the parent name using the following:

    setFields(
        produce((draft) => {
            const field = draft.find((field) => field.id === "parent1");
            field[name] = "new parent1";
        })
    );

How do I go deeper, find within children the child by id and change its content value?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Turns out it is remarkably simple: just mutate the data.

setFields(
    produce((draft) => {
        const child = draft.find((field) => field.id === "parent1").children.find((child)=>child.id==="child21");
        child[content] = "new value";
    })
);
about 4 years ago · Santiago Trujillo 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