React-native-reanimated: rerendering breaks withRepeat animation

Created on 8 Jan 2021  路  3Comments  路  Source: software-mansion/react-native-reanimated

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
馃彔 Reanimated2 馃悶 Bug

All 3 comments

Issue validator - update # 1

Hello!
Congratulations! Your issue passed the validator! Thank you!

can you try this?

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

T

can you try this?

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

this works, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArsalanCsquare picture ArsalanCsquare  路  3Comments

robertgonzales picture robertgonzales  路  3Comments

nextriot picture nextriot  路  3Comments

wasim-abuzaher picture wasim-abuzaher  路  3Comments

mrousavy picture mrousavy  路  3Comments