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.

On state change and component re-rendering animation becomes junky.

See original GitHub issue

First of all thanks for providing this wonderful library.

ISSUE

I am using MotiView for creating Waves animation and every time the component mounts it is working fine and on state update, the animation becomes junky and stops working at the end. I have also wrapped my animation component into the AnimatePresence but still no success.

Code Sample

     <AnimatePresence style={[styles.dot, styles.center]}>
        {[...Array(5).keys()].map(index => {
          return (
            <MotiView
              from={{opacity: 1, scale: 0}}
              animate={{opacity: 0, scale: 9}}
              transition={{
                type: 'timing',
                duration: durationCal,
                easing: Easing.inOut(Easing.ease),
                delay: index * 400,
                repeatReverse: false,
                loop: true,
              }}
              key={index}
              style={[StyleSheet.absoluteFillObject, styles.dot]}
            />
          );
        })}
     </AnimatePresence>

Demo Video

https://user-images.githubusercontent.com/67545083/151162365-ed8ec682-f8ff-4472-9e6d-f8e28f690bb4.mov

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
nandorojocommented, Jan 26, 2022
Screen Shot 2022-01-26 at 10 21 53 AM

I just saw an issue with your code here. You’re using random numbers for key here. That is guaranteed to break code in react. It should always be a stable ID, or an index if indices are stable. This is guaranteeing a full re-render and random behavior. I’m almost certain this is never what you should do.

@nandorojo normal_speed is a number.

In this case, your code should just be this:

const durationCal = normal_speed - waveCounter

Also, rather than a state change, use a shared value. Moti lets you pass the result of useDerivedValue to transition, meaning you don’t have to trigger re-renders and get better performance. You’ll have to split each item into its own component, and then you can do something like this:

const waveCounter = useSharedValue(100)
const onSwipe = () => {
  'worklet'

  // change your waveCounter here somehow
  waveCounter.value = 200
}

// then return each item

// then inside of each item

const transition = useDerivedValue(() => ({
  type: 'timing',
  duration: normal_speed - waveCounter.value,
  easing: Easing.inOut(Easing.ease),
  delay: index * 400,
  repeatReverse: false,
  loop: true
}))

return <MotiView transition={transition} />

Do not use exit or AnimatePresence, they don’t make sense here.

0reactions
nandorojocommented, Feb 25, 2022

Hi @nandorojo actually I am not able to create a reproducible snack because of reanimated2 and gesture-handler version issue.

I believe these do work on Snack for iOS

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing state re-renders the component in a way that breaks ...
this is a switch component that animates between two states. For some reason in the styleguide the component jumps between its two states...
Read more >
Why You Need to Understand Re-rendering in React and ...
State can be confusing to understand at first since it only re-renders when the reference to your state changes. This video explains a...
Read more >
My animation is not working when re rendering my react ...
After React rerender component it compares new render with the old one. And update only part that is changed. So you may try...
Read more >
When does React re-render components? - Felix Gerschau
When the state changes in the parent component (in this case, App ), the two Tile components will re-render, even though the second...
Read more >
React Hooks - Understanding Component Re-renders - Medium
What would happen — when parent and child components have their own local state (via useState) and parent's state gets changed ?
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