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.

[Question] Best practice for using Animated nodes in functional components / hooks

See original GitHub issue

When using animated nodes in functional components or inside custom hooks, simply assigning them to a variable (in every render) seems to work fine, but is there a significant cost or other issue to it?

I started out using useMemo like this, because I thought that would match what I was doing before with class fields:

const saw = useMemo(() => Animated.sub(1, ramp), [ramp]);

But maybe it’s not neccessary at all?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

21reactions
alwexcommented, Aug 17, 2019

Thanks to this video https://www.youtube.com/watch?v=1Q9efh7OcR8 from William Candillon, I managed to get my head around using the library with functional components. Here is my solution highly inspired from the video.

// ...
import Animated, { Easing } from "react-native-reanimated";
import { runTiming, bInterpolate } from "react-native-redash";
import { useMemoOne } from "use-memo-one";

const {
  set,
  Value,
  Clock,
  useCode
};

const MyComponent:React.FC<Props> = ({isVisible, action}) => {
  // using useMemoOne from use-memo-one
  const { animation, clock } = useMemoOne(
    () => ({
      animation: new Value(isVisible ? 0 : 1),
      clock: new Clock()
    }),
    [isVisible]
  );

  useCode(
    set(
      animation,
      // runTiming is a helper from react-native-redash
      runTiming(clock, animation, {
        toValue: selected ? 1 : 0,
        duration: 250,
        easing: Easing.inOut(Easing.ease)
      })
    ),
    [animation]
  );

  // interpolated value, bInterpolate is a helper from react-native-redash
  const transY = bInterpolate(animation, -20, 0);

  return (
    <TouchableOpacity onPress={action}>
      <Animated.View
        style={{ transform: [{ translateY: transY }] }}
      >
        <Text>I slide down or up based on isVisible value</Text>
      </Animated.View>
    </TouchableOpacity>
  );
}

when isVisible change, the animation is restarted and the animation value get interpolated again in the other direction, from 0 to 1, or 1 to 0 depending on the visibility flag.

I hope this would help people having difficulty with functional component implementation and reanimated.

4reactions
Ashkan-Oliaiecommented, Aug 17, 2019

@alwex What about gesture based animations ? because the state change at the middle of running animations seems to interrupt the animation 🤕
or at least in my case 😄 before I get to know useMemoOne , I used to define some animation values outside of the component function so they won’t get rerender and reset every time state changes

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks cheat sheet: Best practices with examples
In this tutorial, we'll outline some React Hooks best practices and highlight some use cases with examples, from simple to advanced scenarios.
Read more >
Let's play with React Hooks: From a Function Component to a ...
In this example I'm going to walk you through adding state and animation to a function component using hooks. I'm making use of...
Read more >
45 Advanced React Interview Questions (With Hooks) Devs ...
Check 45 Advanced React Interview Questions (With Hooks) Devs Must Clarify and Know and Land Your Next Six-Figure Job Offer! 100% Tech Interview...
Read more >
Generating new component types in react hooks, how to ...
I'm asking how to maintain performance while generating functional components inside a hook. Using this pattern would be a nice way to reduce ......
Read more >
React Function Components
As you have seen, React Hooks enable us to use state in React (Arrow) Function Components. Whereas you would have used a setState...
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