Skip to content

Fix dynamic updates of layout animations - #7788

Merged
piaskowyk merged 2 commits into
mainfrom
@piaskowyk/fix-layout-animation-update
Jul 15, 2025
Merged

piaskowyk merged 2 commits into
mainfrom
@piaskowyk/fix-layout-animation-update

Conversation

@piaskowyk

@piaskowyk piaskowyk commented Jul 14, 2025 •

Copy link
Copy Markdown
Member

Summary

This PR adds handling for removing or changing layout animations over a component's lifecycle. In the previous approach, we saved the layout animation during mounting and never changed it.
Note that this only makes sense for layout and exiting animations - we can't change the entering animation during a component's lifetime.

Fixes #7763

before after
Screen.Recording.2025-07-15.at.09.19.55.mov
Screen.Recording.2025-07-15.at.09.19.24.mov

Test plan

code
import { useState } from 'react';
import {
  Button,
  SafeAreaView,
  StyleSheet,
  Text,
} from 'react-native';
import Animated, {
  FadeOutLeft,
} from 'react-native-reanimated';

export default function App() {
  const [isVisible, setIsVisible] = useState(true);
  const [toggleAnimation, setToggleAnimation] = useState(true);

  return (
    <SafeAreaView style={styles.container}>
      <Button
        title={'Toggle Animation'}
        onPress={() => setToggleAnimation(!toggleAnimation)}
      />
      <Button
        title={isVisible ? 'Hide' : 'Show'}
        onPress={() => setIsVisible(prev => !prev)}
      />
      <Text>Has Exiting?: {toggleAnimation ? 'YES' : 'NO'}</Text>
      {isVisible ? (
        <Animated.View
          exiting={toggleAnimation ? FadeOutLeft : undefined}
          style={{ width: 150, height: 150, backgroundColor: 'green' }}
        />
      ) : null}
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  center: {
    alignItems: 'center',
  },
});

@piaskowyk
piaskowyk marked this pull request as ready for review July 15, 2025 07:26
@piaskowyk
piaskowyk added this pull request to the merge queue Jul 15, 2025
Merged via the queue into main with commit bf3a741 Jul 15, 2025
@piaskowyk
piaskowyk deleted the @piaskowyk/fix-layout-animation-update branch July 15, 2025 11:16
github-merge-queue Bot pushed a commit that referenced this pull request Jul 16, 2025
## Summary

This PR fixes the issue introduced in the #7788 PR caused by the invalid
entering animation configuration. The proper entering animation config
was passed only when `skipEntering` was set to `true`, instead of being
passed when it is set to `false` and entering should not be skipped.

## Example recordings

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/7b7f5ce6-9182-4dca-aaa6-97a65c919d39"
/> | <video
src="https://github.com/user-attachments/assets/cfba1681-3852-4f31-b18e-f38dc672b665"
/> |

## Test plan

<details>
<summary>Source code</summary>
```tsx
import React, { useState } from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import Animated, {
  FadeOut,
  FadeIn,
  LayoutAnimationConfig,
} from 'react-native-reanimated';

export default function EmptyExample() {
  const [skipEntering, setSkipEntering] = useState(false);
  const [skipExiting, setSkipExiting] = useState(false);
  const [show, setShow] = useState(true);

  return (
    <View style={styles.container}>
      <Button
        title={skipEntering ? 'Do not skip entering' : 'Skip entering'}
        onPress={() => setSkipEntering(!skipEntering)}
      />
      <Button
        title={skipExiting ? 'Do not skip exiting' : 'Skip exiting'}
        onPress={() => setSkipExiting(!skipExiting)}
      />
      <Button title={show ? 'Hide' : 'Show'} onPress={() => setShow(!show)} />

      <Text>skipEntering: {skipEntering ? 'true' : 'false'}</Text>
      <Text>skipExiting: {skipExiting ? 'true' : 'false'}</Text>

      {show && (
        <LayoutAnimationConfig
          skipEntering={skipEntering}
          skipExiting={skipExiting}>
          <Animated.View
            style={styles.box}
            entering={FadeIn}
            exiting={FadeOut}
          />
        </LayoutAnimationConfig>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'red',
  },
});
```
</details>
MatiPl01 added a commit that referenced this pull request Jul 17, 2025
## Summary

This PR is a cherry-pick of changes applied in the #7788 and #7812 PRs
with some adjustments for proper Paper support.

## Example recordings

### Paper

Worked fine before as well.

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/39eff5ad-d28e-4489-b6ba-5149add35970"
/> | <video
src="https://github.com/user-attachments/assets/f5c33982-595e-4a64-b3f5-ccb7c4e5d4ce"
/> |

### Fabric

Exiting animations could not be changed after the view was mounted. It
had to be remounted to apply the new exiting prop value.

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/822c6c62-088b-4f6d-977c-3d511c354d7f"
/> | <video
src="https://github.com/user-attachments/assets/0086903f-4a4f-4db3-8737-415c57dd290a"
/> |

## Test example

<details>
<summary>Code snippet</summary>

```tsx
import React, { useState } from 'react';
import { Button, StyleSheet, View } from 'react-native';
import Animated, {
  FadeIn,
  FadeOutDown,
  LinearTransition,
} from 'react-native-reanimated';

export default function EmptyExample() {
  const [exitingEnabled, setExitingEnabled] = useState(true);
  const [layoutEnabled, setLayoutEnabled] = useState(true);
  const [show, setShow] = useState(true);
  const [pos, setPos] = useState(0);

  return (
    <View style={styles.container}>
      <Button title="Toggle show" onPress={() => setShow(!show)} />
      <Button title="Toggle pos" onPress={() => setPos(Math.random() * 100)} />
      <Button
        title={`Toggle exiting: ${exitingEnabled}`}
        onPress={() => setExitingEnabled(!exitingEnabled)}
      />
      <Button
        title={`Toggle layout: ${layoutEnabled}`}
        onPress={() => setLayoutEnabled(!layoutEnabled)}
      />
      {show && (
        <Animated.View
          entering={FadeIn}
          exiting={exitingEnabled ? FadeOutDown : undefined}
          layout={layoutEnabled ? LinearTransition : undefined}
          style={{
            top: pos,
            width: 100,
            height: 100,
            backgroundColor: 'red',
          }}
        />
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});
```
</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.

Dynamic updates to layout animations do not apply during component lifetime (Fabric)

2 participants