moti: Bumping/unwanted animation at each render (even if `animate` does not change)

Even if animate prop values don’t change, there’s a bumping animation at each render.

  • Shape component always uses the same animate values.
  • App component triggers a re-render every time you touch the screen.
  • Shape gets animated even if there should not be any animation.

Try this code, and touch the screen:

import * as React from "react";
import { StyleSheet, Pressable } from 'react-native'
import { View } from 'moti'

function Shape({ bg }) {
  return (
    <View
      from={{
        opacity: 0,
        scale: 0.5,
      }}
      animate={{
        opacity: 1,
        scale: 1,
      }}
      exit={{
        opacity: 0,
        scale: 0.9,
      }}
      style={[styles.shape, { backgroundColor: bg }]}
    />
  )
}

export default function App() {
  const [visible, toggle] = React.useReducer((s) => !s, true)

  return (
    <Pressable onPress={toggle} style={styles.container}>
      <Shape bg="hotpink" key="hotpink" />
    </Pressable>
  )
}

const styles = StyleSheet.create({
  shape: {
    justifyContent: 'center',
    height: 250,
    width: 250,
    borderRadius: 25,
    marginRight: 10,
    backgroundColor: 'white',
  },
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
    backgroundColor: '#9c1aff',
  },
})

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 45 (27 by maintainers)

Most upvoted comments

Since it seems like this is due to a bug with Reanimated’s old version, I’m going to close this.

Once Reanimated v2 stable is supported by Expo (upon their release of SDK 41), I’ll remove the velocity: 2 patch. In the meantime, I recommend relying on React.memo (or type: 'timing' 😢), as I mentioned above.

I appreciate you all helping get to the bottom of this!

Both animations should go from opacity 0->1 in a loop (and they do if I don’t click and update state).

https://user-images.githubusercontent.com/1663126/115054337-6b00a780-9ee0-11eb-948a-c63d7e11bc14.mp4