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.

Hook equivalent for `getSnapshotBeforeUpdate`

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

There is no hook based equivalent for getSnapshotBeforeUpdate. The docs state:

Our goal is for Hooks to cover all use cases for classes as soon as possible. There are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet, but we plan to add them soon.

What is the expected behavior?

There is a hook based equivalent for getSnapshotBeforeUpdate, maybe something like:

function ScrollingList(props) {
  const lengthRef = React.useRef(0);
  const listRef = React.useRef(null);

  const prevHeight = React.useSnapshot(() => {
    if (lengthRef.current < props.list.length) {
      const list = listRef.current;
      return list.scrollHeight - list.scrollTop;
    }
  }); 

  React.useEffect(() => {
    lengthRef.current = props.list.length;
  }, [props.list.length]);

  React.useEffect(() => {
    if (prevHeight != null) {
      const list = listRef.current;
      list.scrollTop = list.scrollHeight - prevHeight;
    }
  }, [prevHeight]);
  return (
    <div ref={listRef}>{/* ... */}</div>
  );
}

This code probably is bug-ridden and not the best use of hooks but you get the idea.

I’d like to know:

  1. If this feature is planned or on the roadmap.
  2. What the proposed API will be.
  3. If anyone is working on this.

Sorry, if this is being tracked somewhere and I haven’t seen it. I’m planning an intense component which will use getSnapshotBeforeUpdate and I’d love some guidance about the future of this lifecycle method.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React v16.8.0 and later. All browsers and OSes.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

47reactions
mattgperrycommented, Oct 21, 2020

What’s the current thinking on this? Layout animations are impossible to perform with hooks without this.

16reactions
awearycommented, Jul 16, 2020

Re-opening as it’s still an active feature request

Read more comments on GitHub >

github_iconTop Results From Across the Web

getSnapshotBeforeUpdate using react hooks - Stack Overflow
There are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet, but we plan to add them soon.
Read more >
Hooks FAQ - React
Our goal is for Hooks to cover all use cases for classes as soon as possible. There are no Hook equivalents to the...
Read more >
Capture values using the lifecycle hook ... - Egghead.io
getSnapshotBeforeUpdate is a lifecycle hook that was introduced with React 16.3. It is invoked right before the most recently rendered output is committed ......
Read more >
Converting React Class Component Lifecycle Methods to Hooks
Functional Components are typically easier to read, maintain, and test compared to their Class-based equivalents. However, utilizing Hooks ...
Read more >
Popmotion в Twitter: „Is there an equivalent hook to ...
Is there an equivalent hook to `getSnapshotBeforeUpdate`? ... not yet, its mentioned somewhere in the hooks docs - it has multiple pages so...
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