Skip to content

Commit

Permalink
Update useAnimatedStyle docs (#5159)
Browse files Browse the repository at this point in the history
## Description

This PR explains what `useAnimatedStyle` is for by contrasting to
limitations of [inline
styling](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animations-in-inline-styling)

It also adds missing imports on the ["Animating styles and
props"](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/animating-styles-and-props)
page

Fixes
#5115
  • Loading branch information
kacperkapusciak authored Sep 29, 2023
1 parent 2216f91 commit b40463c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/docs/core/useAnimatedStyle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ sidebar_position: 2

Styles defined using `useAnimatedStyle` have to be passed to `style` property of an [Animated component](/docs/fundamentals/glossary#animated-component). Styles are automatically updated whenever an associated shared value or React state changes.

In contrast to the [inline styling](/docs/fundamentals/glossary#animations-in-inline-styling), `useAnimatedStyle` allows to [access values stored in shared values](/docs/fundamentals/animating-styles-and-props/#animating-styles) in the styles object it defines.

For animating properties use [`useAnimatedProps`](/docs/core/useAnimatedProps) instead.

## Reference
Expand Down
15 changes: 13 additions & 2 deletions docs/docs/fundamentals/animating-styles-and-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ In [the last section](/docs/fundamentals/your-first-animation), we learned how t
As we learned in the previous section we can animate styles by [passing shared values inline](/docs/fundamentals/glossary#animations-in-inline-styling) to the elements' `style` property:

```jsx
import Animated, { useSharedValue } from 'react-native-reanimated';

function App() {
const width = useSharedValue(100);

Expand All @@ -26,16 +28,21 @@ In basic cases, this syntax works well but it has one big downside. It doesn't a

Let's suppose we have an example with a box which moves to the right on every button press:

```jsx {5,10}
```jsx
import { View, Button } from 'react-native';
import Animated, { useSharedValue, withSpring } from 'react-native-reanimated';

function App() {
const translateX = useSharedValue(0);

const handlePress = () => {
// highlight-next-line
translateX.value = withSpring(translateX.value + 50);
};

return (
<View style={styles.container}>
{/* highlight-next-line */}
<Animated.View style={[styles.box, { transform: [{ translateX }] }]} />
<Button onPress={handlePress} title="Click me" />
</View>
Expand Down Expand Up @@ -83,12 +90,16 @@ const AnimatedCircle = Animated.createAnimatedComponent(Circle);

To animate the radius of the SVG circle we can simply pass the shared value as a prop:

```jsx {6}
```jsx
import { useSharedValue } from 'react-native-reanimated';
import { Svg } from 'react-native-svg';

function App() {
const r = useSharedValue(10);

return (
<Svg>
{/* highlight-next-line */}
<AnimatedCircle cx="50" cy="50" r={r} fill="blue" />
</Svg>
);
Expand Down

0 comments on commit b40463c

Please sign in to comment.