question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No optimization for rerendering when a global state is hooked via nested state

See original GitHub issue

I’m trying to build a “form management” library, that allows to structure the form fields in deeply nested objects like this:

{
  "field1": {
    "id": "field1",
    "label": "Label for field1",
    "value": "Initial value for field1"
  },
  "field2": {
    "id": "field2",
    "label": "Label for field2",
    "value": "Initial value for field2"
  },
  "section": {
    "field1": {
      "id": "section.field1",
      "label": "Label for section.field1",
      "value": "Initial value for section.field1"
    }
  }
}

The form fields themselves are complex objects, too.

Forms are composed by specifying a name attribute on the field components, e.g. <Field name="deeply.nested.sub.field" />. Because state.nested("deeply.nested.sub.field") is not supported, I wrote a function to split that property path and then iteratively call nested on all parts.

When I edit “section.field1” it seems to work, but when I edit “field1” the “section.field1” component also receives an update. You can find the code here: https://codesandbox.io/s/stupefied-joliot-mkkhl?file=/src/App.tsx

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
avkonstcommented, Sep 20, 2021

verified in your sandbox and in unit tests

1reaction
avkonstcommented, Sep 20, 2021

your usecase is fully supported with with hookstate 3.0.12 version

Read more comments on GitHub >

github_iconTop Results From Across the Web

Just Say No to Excessive Re-Rendering in React - GrapeCity
In this article, we will address instances of excessive re-rendering in React applications and demonstrate how to avoid them.
Read more >
Preventing rerenders with React.memo and useContext hook.
I can't rely on data from context API by using (useContext hook) to prevent unnecessary rerenders with React.memo.
Read more >
5 Ways to Avoid React Component Re-Renderings
These Hooks reduce re-renderings by caching and returning the same result if the inputs are the same without any computations. When the inputs...
Read more >
When does React re-render components? - Felix Gerschau
React re-rendering explained and how to optimize React's re-render performance.
Read more >
React Hooks - Understanding Component Re-renders - Medium
useState hook is the primary building block which enables functional components to hold state between re-renders. Let's understand the workings ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found