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.

Hooks + animations = how?

See original GitHub issue

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

I doubt it’s a bug, so it’s either a missing feature or a request for clarification on how to use/not to use hooks.

What is the current behavior?

I’m trying to design an interactive component with animation event listeners. As highlighted by this issue, state updates behave differently when triggered from events than from other sources. In this case I want to use requestAnimationFrame to incrementally update a value using a setter function.

This is my best effort so far (after a lot of tinkering):

const useAnimationFrame = callback => {
  let isDisplaying = true;
  const loop = () => {
    if (!isDisplaying) {
      return;
    }
    callback();
    requestAnimationFrame(loop);
  };
  useEffect(() => {
    requestAnimationFrame(loop);
    return () => {
      isDisplaying = false;
    };
  });
};

It sort of works, but I’m not sure it’s ideal. Here’s the fiddle: https://codesandbox.io/s/0y609q54pw. Is this really how I’m supposed to be doing it? I use this approach in a complex component (an image carousel) and the performance is noticably worse than the this.setState traditional approach counterpart.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
gaearoncommented, Nov 13, 2018

We generally advise against mutating things during rendering. That can cause issues in concurrent mode (to be released later) and with server rendering.

2reactions
ajaymorecommented, May 24, 2019

I think maybe this should work? https://codesandbox.io/s/ojxl32jm4z

It’s a bit hard to diagnose performance problems when the demo doesn’t reproduce them. 😃 If you provide a more realistic demo it would be easier to help.

@gaearon

useMutationEffect is not available in react:16.8.6 Can you help me with this code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Animations with React Hooks - Medium
CSS Animations with React Hooks. Animate your application with cleaner state changes and attribute selectors. Welcome to the ...
Read more >
useAnimation React Hook - useHooks
This hook allows you to smoothly animate any value using an easing function (linear, elastic, etc). In the example we call the useAnimation...
Read more >
Animations using React Hooks and GreenSock
Learn how to implement powerful animations using React Hooks and GreenSock in this tutorial, which covers basic and advanced concepts.
Read more >
How to Trigger Animations with React Hooks | by Andrew Bliss
In this article let's go over how to trigger animations using React Hooks. We want to be able to control when the user...
Read more >
How to build a reusable animation component using React ...
React Hooks are a new feature in React 16.8. They offer a simpler approach to lifecycle and state management in React components. The...
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