Skip to content
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

Merge initial style #4711

Merged
merged 1 commit into from
Jul 11, 2023
Merged

Merge initial style #4711

merged 1 commit into from
Jul 11, 2023

Conversation

piaskowyk
Copy link
Member

Summary

Previously we saved only the last of the animated style passed to the component. In effect, we can lose the previously applied style after render. This PR adds the possibility to save more than one animated style as the initial style for component.

<Animated.View
  style={[
    { height: 10, backgroundColor: 'black', margin: 30 },
    animatedStyle1, // <-- this one will be lost
    animatedStyle2, // <-- only this one will be saved
  ]}
/>
Before After
Screen.Recording.2023-07-11.at.14.33.54.mov
Screen.Recording.2023-07-11.at.14.34.27.mov

Example code

code
import Animated, {
  useSharedValue,
  withTiming,
  useAnimatedStyle,
} from 'react-native-reanimated';
import { View, Button, Text } from 'react-native';
import React, { useState } from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  const [counter, setCounter] = useState(0);
  const width = useSharedValue(100);
  const height = useSharedValue(100);

  const widthStyle = useAnimatedStyle(() => {
    return {
      width: withTiming(width.value),
    };
  });

  const heightStyle = useAnimatedStyle(() => {
    return {
      height: withTiming(height.value),
    };
  });

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
      }}>
      <Text>Counter: {counter}</Text>
      <Animated.View
        style={[
          { height: 10, backgroundColor: 'black', margin: 30 },
          heightStyle,
          widthStyle,
        ]}
      />
      <Button
        title="update width"
        onPress={() => {
          width.value = Math.random() * 300;
          setCounter(counter + 1)
        }}
      />
      <Button
        title="update height"
        onPress={() => {
          height.value = Math.random() * 300;
        }}
      />
    </View>
  );
}

Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@piaskowyk piaskowyk added this pull request to the merge queue Jul 11, 2023
Merged via the queue into main with commit 1322453 Jul 11, 2023
1 check passed
@piaskowyk piaskowyk deleted the @piaskowyk/fix-initialStyle branch July 11, 2023 13:28
Latropos pushed a commit that referenced this pull request Jul 13, 2023
## Summary

Previously we saved only the last of the animated style passed to the
component. In effect, we can lose the previously applied style after
render. This PR adds the possibility to save more than one animated
style as the initial style for component.

```js
<Animated.View
  style={[
    { height: 10, backgroundColor: 'black', margin: 30 },
    animatedStyle1, // <-- this one will be lost
    animatedStyle2, // <-- only this one will be saved
  ]}
/>
```

|Before|After|
|--|--|
| <video
src="https://github.com/software-mansion/react-native-reanimated/assets/36106620/d88df2c7-cd23-4ed4-ac70-fa15dbba240f"
/> | <video
src="https://github.com/software-mansion/react-native-reanimated/assets/36106620/42a17972-9c60-4f0c-a7a1-f7e42471ca70"
/> |

## Example code

<details>
<summary>code</summary>

```js
import Animated, {
  useSharedValue,
  withTiming,
  useAnimatedStyle,
} from 'react-native-reanimated';
import { View, Button, Text } from 'react-native';
import React, { useState } from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  const [counter, setCounter] = useState(0);
  const width = useSharedValue(100);
  const height = useSharedValue(100);

  const widthStyle = useAnimatedStyle(() => {
    return {
      width: withTiming(width.value),
    };
  });

  const heightStyle = useAnimatedStyle(() => {
    return {
      height: withTiming(height.value),
    };
  });

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
      }}>
      <Text>Counter: {counter}</Text>
      <Animated.View
        style={[
          { height: 10, backgroundColor: 'black', margin: 30 },
          heightStyle,
          widthStyle,
        ]}
      />
      <Button
        title="update width"
        onPress={() => {
          width.value = Math.random() * 300;
          setCounter(counter + 1)
        }}
      />
      <Button
        title="update height"
        onPress={() => {
          height.value = Math.random() * 300;
        }}
      />
    </View>
  );
}
```

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants