-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix dynamically added/removed animated styles on iOS/Android #5268
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { Text, StyleSheet, View, Button } from 'react-native'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import Animated, { | ||
| useAnimatedStyle, | ||
| useSharedValue, | ||
| withTiming, | ||
| } from 'react-native-reanimated'; | ||
|
|
||
| export default function DynamicStylesExample() { | ||
| const [extraStyle, setExtraStyle] = React.useState(false); | ||
| const [rerender, setRerender] = React.useState(false); | ||
|
|
||
| const widthShared = useSharedValue(80); | ||
| const backgroundColorShared = useSharedValue<string>('rgb(255,127,63)'); | ||
|
|
||
| const animatedWidthStyle = useAnimatedStyle(() => ({ | ||
| width: widthShared.value, | ||
| })); | ||
| const animatedBackgroundColorStyle = useAnimatedStyle(() => { | ||
| return { | ||
| backgroundColor: backgroundColorShared.value, | ||
| }; | ||
| }); | ||
|
|
||
| function handleChangeProps() { | ||
| setExtraStyle((extraStyle) => !extraStyle); | ||
| } | ||
tjzel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function handleAnimate() { | ||
| widthShared.value = withTiming((Math.random() * 160) / 2 + 40, { | ||
| duration: 1000, | ||
| }); | ||
|
|
||
| const r = Math.random() * 256; | ||
| const g = Math.random() * 256; | ||
| const b = Math.random() * 256; | ||
|
|
||
| backgroundColorShared.value = withTiming(`rgb(${r},${g},${b})`, { | ||
| duration: 1500, | ||
| }); | ||
| } | ||
|
|
||
| function handleForceRerender() { | ||
| setRerender(!rerender); | ||
| } | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <Text style={styles.text}>State: {String(rerender)}</Text> | ||
| <View style={styles.columnContainer}> | ||
| <View style={styles.column}> | ||
| <Text style={styles.text}>InlineStyles</Text> | ||
|
|
||
| <Text style={styles.text}>Same length array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
tjzel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| style={[ | ||
| styles.box, | ||
| { width: widthShared }, | ||
| extraStyle ? { backgroundColor: backgroundColorShared } : null, | ||
| ]} | ||
| /> | ||
|
|
||
| <Text style={styles.text}>Different length array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={ | ||
| extraStyle | ||
| ? [ | ||
| styles.box, | ||
| { width: widthShared }, | ||
| { backgroundColor: backgroundColorShared }, | ||
| ] | ||
| : [styles.box, { width: widthShared }] | ||
| } | ||
| /> | ||
| <Text style={styles.text}>Same length extra plain style array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={[ | ||
| styles.box, | ||
| { width: widthShared }, | ||
| extraStyle ? { backgroundColor: backgroundColorShared } : null, | ||
| extraStyle ? styles.radius : null, | ||
| ]} | ||
| /> | ||
| <Text style={styles.text}> | ||
| Different length extra plain style array | ||
| </Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={ | ||
| extraStyle | ||
| ? [ | ||
| styles.box, | ||
| { width: widthShared }, | ||
| { backgroundColor: backgroundColorShared }, | ||
| styles.radius, | ||
| ] | ||
| : [styles.box, { width: widthShared }] | ||
| } | ||
| /> | ||
| <Text style={styles.text}> | ||
| Two animated styles independent of state | ||
| </Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={[ | ||
| styles.box, | ||
| { width: widthShared }, | ||
| { backgroundColor: backgroundColorShared }, | ||
| ]} | ||
| /> | ||
| </View> | ||
| <View style={styles.column}> | ||
| <Text style={styles.text}>useAnimatedStyle</Text> | ||
|
|
||
| <Text style={styles.text}>Same length array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={[ | ||
| styles.box, | ||
| animatedWidthStyle, | ||
| extraStyle ? animatedBackgroundColorStyle : null, | ||
| ]} | ||
| /> | ||
| <Text style={styles.text}>Different length array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={ | ||
| extraStyle | ||
| ? [styles.box, animatedWidthStyle, animatedBackgroundColorStyle] | ||
| : [styles.box, animatedWidthStyle] | ||
| } | ||
| /> | ||
| <Text style={styles.text}>Same length extra plain style array</Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={[ | ||
| styles.box, | ||
| animatedWidthStyle, | ||
| extraStyle ? animatedBackgroundColorStyle : null, | ||
| extraStyle ? styles.radius : null, | ||
| ]} | ||
| /> | ||
| <Text style={styles.text}> | ||
| Different length extra plain style array | ||
| </Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={ | ||
| extraStyle | ||
| ? [ | ||
| styles.box, | ||
| animatedWidthStyle, | ||
| animatedBackgroundColorStyle, | ||
| styles.radius, | ||
| ] | ||
| : [styles.box, animatedWidthStyle] | ||
| } | ||
| /> | ||
| <Text style={styles.text}> | ||
| Two animated styles independent of state | ||
| </Text> | ||
| <Animated.View | ||
| accessible={rerender} | ||
| style={[ | ||
| styles.box, | ||
| animatedWidthStyle, | ||
| animatedBackgroundColorStyle, | ||
| ]} | ||
| /> | ||
| </View> | ||
| </View> | ||
|
|
||
| <Text style={styles.text}> | ||
| {extraStyle ? '' : 'no '}extra animated style | ||
| </Text> | ||
| <Button title="Add/remove animated style" onPress={handleChangeProps} /> | ||
| <Button title="Force rerender" onPress={handleForceRerender} /> | ||
| <Button title="Animate" onPress={handleAnimate} /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| columnContainer: { | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-around', | ||
| width: '100%', | ||
| }, | ||
| box: { | ||
| height: 50, | ||
| width: 50, | ||
| borderWidth: 1, | ||
| }, | ||
| radius: { | ||
| borderWidth: 10, | ||
| }, | ||
| column: { | ||
| maxWidth: '50%', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| text: { | ||
| maxWidth: 180, | ||
| textAlign: 'center', | ||
| }, | ||
| }); | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.