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.

Avoid re-rendering components if selector value hasn't changed.

See original GitHub issue

tl;dr: if an atom is an object and a selector returns a single property from that object, will downstream components re-render whenever ANY part of the atom changes, or only when that selector value changes? i.e. are selector output values cached or do re-renders occur whenever any of their input dependencies change? (I suspect the latter?).

If recoil doesn’t cache selector output, then I suspect my best approach is useRecoilCallback to avoid subscribing, then have something like useStateSlice() which takes a slice fn() which triggers component updates (useState/setState) when the slice value differs?

seem right?


background:

This arises from a common(?) case:

The “atom” in my case is a (firebase) document which is basically a property bag of fields. We get async updates from firebase when that data is changed (possibly external to our app - e.g. by another user, etc.).

So if my atom is the document, I (think?) I’m hosed - any update to any field of the document will cause all downstream selectors to update and therefore all components depending on ANY part of the document to re-render.

What I’d like is to think of selectors as a “slicing” the data, so I could have a lastName selector which picks the lastName field out of the Person document. Components depending on lastName would update only when that field is different, ignoring changes to other parts of the person.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:51
  • Comments:39 (14 by maintainers)

github_iconTop GitHub Comments

42reactions
drarmstrcommented, Jul 17, 2020

Yup, I agree that we really want to do this.

35reactions
drarmstrcommented, Jun 12, 2020

Yes, it’s on our roadmap to have selectors suppress downstream updates and subscriptions if their computed value has not changed. I already have a diff for suppressing updates if setting an atom to the same value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent re-rendering of components that have not ...
I would like to prevent the re-rendering and only re-render the component that actually changes. I have seen that useCallback is the right...
Read more >
5 Ways to Avoid React Component Re-Renderings
1. Memoization using useMemo() and UseCallback() Hooks ... Memoization enables your code to re-render components only if there's a change in the props....
Read more >
React re-renders guide: everything, all at once - Developer way
There is no way to prevent a component that uses a portion of Context value from re-rendering, even if the used piece of...
Read more >
React | How to stop re-rendering in React Components
A selector is not recomputed unless one of its arguments changes. Selectors are composable. They can be used as input to other selectors....
Read more >
How to prevent re-renders on React functional components ...
The answer is yes! Use React.memo() to prevent re-rendering on React function components. First, if you' ...
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