React-native-reanimated: Animation resets and stops responding after redux re render state

Created on 14 Jan 2020  路  2Comments  路  Source: software-mansion/react-native-reanimated

I'm trying to implement a custom scroll indicator for a horizontal ScrollView and everything works fine execept when I need to do something on redux that changes the state, the animation resets and stops responding like the gif bellow:

animated

I was able to resolve this creating the animated variables outside of the function, but I don't understand why did it happen, there's other way to resolve this?

Snack Example: https://snack.expo.io/@zlg/daa4e9

Most helpful comment

You need to wrap your Animated Value into a useMemoOne hook to not redeclare your animated value in every rerender.
Your line:

const scrollX = new Value(0);

Should become:

const { scrollX }聽= useMemoOne(() => ({
    scrollX: new Value(0)
}), [])

You could achieve the same with React.useMemo, but useMemoOne https://github.com/alexreardon/use-memo-one is a better alternative because it guarantees that your animated value will never be discarded whereas it can potentially happen with React.useMemo

All 2 comments

You need to wrap your Animated Value into a useMemoOne hook to not redeclare your animated value in every rerender.
Your line:

const scrollX = new Value(0);

Should become:

const { scrollX }聽= useMemoOne(() => ({
    scrollX: new Value(0)
}), [])

You could achieve the same with React.useMemo, but useMemoOne https://github.com/alexreardon/use-memo-one is a better alternative because it guarantees that your animated value will never be discarded whereas it can potentially happen with React.useMemo

Thank you very much @PierreCapo, useMemoOne solved my problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nextriot picture nextriot  路  3Comments

bdrobinson picture bdrobinson  路  3Comments

dinhmai74 picture dinhmai74  路  3Comments

WeTruck picture WeTruck  路  3Comments

ShaMan123 picture ShaMan123  路  3Comments