Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!