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.

Provide more ways to bail out inside Hooks

See original GitHub issue

There’s a few separate issues but I wanted to file an issue to track them in general:

  • useState doesn’t offer a way to bail out of rendering once an update is being processed. This gets a bit weird because we actually process updates during the rendering phase. So we’re already rendering. But we could offer a way to bail on children. Edit: we now do bail out on rendering children if the next state is identical.
  • useContext doesn’t let you subscribe to a part of the context value (or some memoized selector) without fully re-rendering. Edit: see https://github.com/facebook/react/issues/15156#issuecomment-474590693 for solutions to this.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:130
  • Comments:140 (40 by maintainers)

github_iconTop GitHub Comments

92reactions
brunolemoscommented, Nov 19, 2018

I would enjoy something like this to avoid unnecessary re-renders:

const { data } = useContext(MyContext, result => [result.data])

where the second parameter would work like the second parameter of useEffect, except it’s () => [] instead of [], with result being the response of useContext(MyContext).

Note: This is supposing the existing second param could change or react internally could check the typeof to see if it’s a function or the observedBits

72reactions
sebmarkbagecommented, Dec 18, 2018

My personal summary is that new context is ready to be used for low frequency unlikely updates (like locale/theme). It’s also good to use it in the same way as old context was used. I.e. for static values and then propagate updates through subscriptions. It’s not ready to be used as a replacement for all Flux-like state propagation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks API Reference - React
If you update a State Hook to the same value as the current state, React will bail out without rendering the children or...
Read more >
React useReducer Hook ultimate guide - LogRocket Blog
The dispatch method; Bailing out of a dispatch. Building a simple counter app with the useReducer Hook; useState vs. useReducer.
Read more >
How Do React Hooks Actually Work? React.js Deep Dive #3
In this video, we will learn how React Hooks actually work. ... (in other words Why can't they be called inside loops or...
Read more >
تويتر \ Michael Jackson على تويتر: "@sebmarkbage @mfpiccolo ...
Just read an article about React Hooks where the author claims that you ... Provide more ways to bail out inside Hooks ·...
Read more >
State management with React Hooks and Reusable - Medium
It introduces a wrapper hell, no flexibility in dependency order, and can't use a selector and bail out of rendering.
Read more >

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 Tweet

No results found

github_iconTop Related Hashnode Post

No results found