-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: withRepeat
using array not reversing
#6881
Merged
patrycjakalinska
merged 2 commits into
main
from
@patrycjakalinska/withRepeat-not-reversing
Jan 30, 2025
Merged
fix: withRepeat
using array not reversing
#6881
patrycjakalinska
merged 2 commits into
main
from
@patrycjakalinska/withRepeat-not-reversing
Jan 30, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withRepeat
using array not reversing
tjzel
approved these changes
Jan 10, 2025
piaskowyk
approved these changes
Jan 15, 2025
tjzel
pushed a commit
that referenced
this pull request
Feb 17, 2025
## Summary Fixes #6799. Shared value which contains a number array will not behave as expected with a `withRepeat` hook. Because it was an array the `animation.startValue` and `nextAnimation.current` became connected by reference, and therefore `startValue` was rising as `current` instead of staying a actual `startValue`. I discovered that in `arrayOnStart`, `animation.current === value` caused a referenced so i spread it. ## Test plan Example code: ```js import { Text, SafeAreaView, StyleSheet } from 'react-native'; import { useEffect } from 'react'; import { useAnimatedReaction, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated'; export default function App() { const arrayValue = useSharedValue([0]); const objValue = useSharedValue({x:0}); const numberValue = useSharedValue(0); const textValue = useSharedValue('0'); useEffect( () => { const duration = 300; // ❌ changes from [0,0,0] to [30,30,30] but then stops arrayValue.value = withRepeat( withTiming([30], { duration} ), 2, true); // ✅ repeats from 0 to 30 and back to 0 numberValue.value = withRepeat( withTiming(30, { duration} ), 2, true); // ✅ repeats from {x:0,y:0,z:0} to {x:30,y:30,z:30} and back to {x:0,y:0,z:0} objValue.value = withRepeat( withTiming({x: 30}, { duration} ), 2, true); },[]); useAnimatedReaction( () => [arrayValue.value, numberValue.value, objValue.value], ([arrayValue, numberValue, objValue]) => { // ✅ works - value reaches 30, then back to 0 // textValue.value = `${numberValue.toFixed(2)}`; // ✅ works - value reaches 30, then back to 0 // textValue.value = `${objValue.x.toFixed(2)}`; // ❌ value reaches 30, then remains textValue.value = `${arrayValue[0].toFixed(2)}` console.log('value:', textValue.value); } ) return ( <SafeAreaView style={styles.container}> <Text style={styles.text}>See console.log</Text> </SafeAreaView> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#ecf0f1', padding: 8, }, text: { margin: 24, fontSize: 18, fontWeight: 'bold', textAlign: 'center', } }); ```
tjzel
pushed a commit
that referenced
this pull request
Feb 17, 2025
## Summary Fixes #6799. Shared value which contains a number array will not behave as expected with a `withRepeat` hook. Because it was an array the `animation.startValue` and `nextAnimation.current` became connected by reference, and therefore `startValue` was rising as `current` instead of staying a actual `startValue`. I discovered that in `arrayOnStart`, `animation.current === value` caused a referenced so i spread it. ## Test plan Example code: ```js import { Text, SafeAreaView, StyleSheet } from 'react-native'; import { useEffect } from 'react'; import { useAnimatedReaction, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated'; export default function App() { const arrayValue = useSharedValue([0]); const objValue = useSharedValue({x:0}); const numberValue = useSharedValue(0); const textValue = useSharedValue('0'); useEffect( () => { const duration = 300; // ❌ changes from [0,0,0] to [30,30,30] but then stops arrayValue.value = withRepeat( withTiming([30], { duration} ), 2, true); // ✅ repeats from 0 to 30 and back to 0 numberValue.value = withRepeat( withTiming(30, { duration} ), 2, true); // ✅ repeats from {x:0,y:0,z:0} to {x:30,y:30,z:30} and back to {x:0,y:0,z:0} objValue.value = withRepeat( withTiming({x: 30}, { duration} ), 2, true); },[]); useAnimatedReaction( () => [arrayValue.value, numberValue.value, objValue.value], ([arrayValue, numberValue, objValue]) => { // ✅ works - value reaches 30, then back to 0 // textValue.value = `${numberValue.toFixed(2)}`; // ✅ works - value reaches 30, then back to 0 // textValue.value = `${objValue.x.toFixed(2)}`; // ❌ value reaches 30, then remains textValue.value = `${arrayValue[0].toFixed(2)}` console.log('value:', textValue.value); } ) return ( <SafeAreaView style={styles.container}> <Text style={styles.text}>See console.log</Text> </SafeAreaView> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#ecf0f1', padding: 8, }, text: { margin: 24, fontSize: 18, fontWeight: 'bold', textAlign: 'center', } }); ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6799. Shared value which contains a number array will not behave as expected with a
withRepeat
hook. Because it was an array theanimation.startValue
andnextAnimation.current
became connected by reference, and thereforestartValue
was rising ascurrent
instead of staying a actualstartValue
. I discovered that inarrayOnStart
,animation.current === value
caused a referenced so i spread it.Test plan
Example code: