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.

useCallback() invalidates too often in practice

See original GitHub issue

This is related to https://github.com/facebook/react/issues/14092, https://github.com/facebook/react/issues/14066, https://github.com/reactjs/rfcs/issues/83, and some other issues.

The problem is that we often want to avoid invalidating a callback (e.g. to preserve shallow equality below or to avoid re-subscriptions in the effects). But if it depends on props or state, it’s likely it’ll invalidate too often. See https://github.com/facebook/react/issues/14092#issuecomment-435907249 for current workarounds.

useReducer doesn’t suffer from this because the reducer is evaluated directly in the render phase. @sebmarkbage had an idea about giving useCallback similar semantics but it’ll likely require complex implementation work. Seems like we’d have to do something like this though.

I’m filing this just to acknowledge the issue exists, and to track further work on this.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:154
  • Comments:109 (22 by maintainers)

github_iconTop GitHub Comments

69reactions
sophiebitscommented, Nov 19, 2018

@sokra An alternate would be:

function useEventCallback(fn) {
  let ref = useRef();
  useLayoutEffect(() => {
    ref.current = fn;
  });
  return useCallback(() => (0, ref.current)(), []);
}

This doesn’t require the args like yours has. But again, you can’t call this in the render phase and the use of mutation is dicey for concurrent.

52reactions
gaearoncommented, May 4, 2022

We’ve submitted a proposal to solve this. Would appreciate your input!

https://github.com/reactjs/rfcs/pull/220

Read more comments on GitHub >

github_iconTop Results From Across the Web

React- useCallback Invalidates Too Often in Practice - Medium
We wrap onClick with useCallback to ensure PureHeavyComponent renders only once. Here the pure heavy component is not re-rendered on any render ...
Read more >
React Hooks- useCallback() Invalidates Too Often in Practice ...
VS Code's tsserver was deleted by another application such as a misbehaving virus detection tool. Please reinstall VS Code. Manage Extension.
Read more >
react hooks: how to handle co-dependent useCallbacks
after some research, looks like the solution for (useCallback() invalidates too often in practice) also fixes this. The general idea is to ...
Read more >
Hooks FAQ - React
What do Hooks mean for popular APIs like Redux connect() and React Router? ... How to read an often-changing value from useCallback? Under...
Read more >
react usecallback - You.com | The Search Engine You Control
class Foo extends Component { handleClick() { console.log('Click happened'); } render() { return ... React- useCallback Invalidates Too Often in Practice.
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 Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found