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.

rerendering breaks withRepeat animation

See original GitHub issue

Description

I have withRepeat animation and some useState value.

const [state, setState] = useState(0);
const val = useSharedValue(1);
val.value = withRepeat(withTiming(1.5, { duration: 2000 }), -1, true);

Any state change will fix the val.value range between 1.5 and the value at rerendering moment

Video

https://user-images.githubusercontent.com/39302602/104065268-850b5980-5210-11eb-9214-107530d87b67.mp4

Steps To Reproduce

  1. Create withRepeat animation
  2. Add some state
  3. Update state

Expected behavior

val.value will change in [1, 1.5] range

Actual behavior

val.value changes in [value at the rerendering moment, 1.5] range

Snack or minimal code example

function App() {
  const [state, setState] = useState(0);
  const val = useSharedValue(1);
  val.value = withRepeat(withTiming(1.5, { duration: 2000 }), -1, true);

  const debug = useDerivedValue(() => {
    console.log(val.value);
    return val.value;
  });

  const style = useAnimatedStyle(() => {
    return {
      transform: [{ scale: val.value }],
    };
  });

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.element, style]} />
      <Button title="useless button" onPress={() => setState(state + 1)} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  element: {
    backgroundColor: "red",
    width: 100,
    height: 100,
    marginBottom: 50,
  },
});

Package versions

  • React: 16.13.1
  • React Native: 0.63.4
  • React Native Reanimated: 2.0.0-alpha.9.2
  • NodeJS: 14.15.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
chakrihackercommented, Jan 9, 2021

can you try this?

useEffect(() => {
    val.value = withRepeat(withTiming(1.5, { duration: 2000 }), -1, true);
}, [])
1reaction
amialonecommented, Mar 25, 2021

can you try this?

useEffect(() => {
    val.value = withRepeat(withTiming(1.5, { duration: 2000 }), -1, true);
}, [])

Use withRepeat without useEffect works sometimes, but I don’t know why Should I use withRepeat wrap with useEffect every time?

Read more comments on GitHub >

github_iconTop Results From Across the Web

withRepeat | React Native Reanimated - Software Mansion
Repeats the provided animation several times. ... be called when all repetitions of the provided animation are complete or when withRepeat is cancelled....
Read more >
Rerendering componet breaks animations (React Native ...
- When the component rerenders it breaks the gesture-animation setup. It seems to me that, gesture stays the same and gets the events...
Read more >
My animation is not working when re rendering my react ...
So, my problem is that I have a component, I associated an animation to it and it is working when the component is...
Read more >
react-native-reanimated: Versions - Openbase
Added measure and scrollTo for web #3661; [Fix] Ignore layout animation props ... fix: breaking change in babel by @hosseinmd in ... rerender...
Read more >
5 React Native animations made easy - Bam Tech
Whilst animating the opacity, use the combination of three utils: withRepeat: animates infinitely in both directions; withSequence: creates a ...
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