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.

UseTransition() and Reanimated 1.3.0

See original GitHub issue

I am getting a weird behaviour with different animations after updating to react-native-reanimated 1.3.0.

Here my specs:

"react-native-gesture-handler": "^1.4.0",
"react-native-redash": "^8.1.2",
"react-native-reanimated": "1.3.0",
"react-native": "0.61.1",
"react": "16.9.0",

The animation gets triggered every time as shown in the video below:

ezgif com-video-to-gif

Here the code:

    const [isOnFocus, setOnFocus] = useState<0 | 1>(0);
    let animatedValue = isOnFocus || value ? 1 : 0;
    const transition = useTransition(
      animatedValue,
      not(animatedValue),
      animatedValue,
      160,
      Easing.ease,
    );
    const inputRange = [0, 1];
    const fontSize = interpolate(transition, {
      inputRange,
      outputRange: [18, 10],
    });
    const top = interpolate(transition, {
      inputRange,
      outputRange: [20, 5],
    });
 
    return (...); 

It used to work with Reanimated 1.2.0, but I need to upgrade it due to other problems.

Any suggestions? Am I missing something? Thanks for the help in advance 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
wcandilloncommented, Oct 24, 2019

This particular issue didn’t appear to be fixed, will test it further

3reactions
lucaspelloni2commented, Oct 7, 2019

We found the solution together with @warrioru. The problem of react-native-reanimated 1.3.0 is that is causes a re-render in the hook everytime the animatedValue gets updated.

the soluton was to introduce a componentDidUpdate hook for checking the prev state. the animation happens now only when the animated state changes.

function usePrevious(x: any) {
  // The ref object is a generic container whose current property is mutable ...
  // ... and can hold any value, similar to an instance property on a class
  const ref = useRef();

  // Store current value in ref
  useEffect(() => {
    ref.current = x;
  }, [x]); // Only re-run if value changes

  // Return previous value (happens before update in useEffect above)
  return ref.current;
}

    let animatedValue = isOnFocus || value ? 1 : 0;
    const prev = usePrevious(animatedValue);
    const transition = useTransition(
      animatedValue,
      not(animatedValue),
      animatedValue,
      400,
      Easing.ease,
    );
    const inputRange = [0, 1];

    let fontSize, top;
    if (prev !== animatedValue) {
      fontSize = interpolate(transition, {
        inputRange,
        outputRange: [FONT_SIZE, FONT_SIZE - 5],
      });
      top = interpolate(transition, {
        inputRange,
        outputRange: [normalize(18), 10],
      });
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

93 Versions - react-native-reanimated - npm
More powerful alternative to Animated library for React Native.. Latest version: 2.13.0, last published: a month ago.
Read more >
Getting started - React Navigation
React Navigation is born from the React Native community's need for an extensible yet easy-to-use navigation solution written entirely in JavaScript (so you ......
Read more >
useTransition and useDeferredValue in React 18
We can use the useTransition hook to tell React that a certain state change will result in an expensive rendering.
Read more >
William Candillon on Twitter: "Reanimated 1.3.0 ships support ...
Reanimated 1.3.0 ships support for string animation values on Android. We'll celebrate it on YouTube by animating some bezier curves.
Read more >
React 18's useTransition() Hook: Master in 2 Minutes
0. React v18 is a newly released version of React, which introduces many new features like Automatic Batching, new APIs like useTransition() ,...
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