Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: How to selectively enable LayoutAnimation

I'd like to enable layout animation for some components but it once it is activated, all components being rendered are affected by layout animation. For example, I have

<container>
  <waterfall/>
  <menu/>
</container>

I only wish to use layout animation for the component menu but the animation is applied to the waterfall rendering which already has its own animation code.

Is this possible with react native?

like image 206
Vu Dang Avatar asked Oct 24 '16 15:10

Vu Dang


People also ask

What is layout animation in React Native?

Automatically animates views to their new positions when the next layout happens. A common way to use this API is to call it before updating the state hook in functional components and calling setState in class components.

What is layout in 3D animation?

What is the layout in animation? Layout in 3D animation is basically a 3D version of the animatic in low res with proxy models. A proxy model is a basic representation of the final 3D model with the right shape and size, used to demonstrate the story visually.


1 Answers

I ran into a similar problem, here's how I solved it with layoutanimation:

Explanation

Keep a state variable in the container component that is passed as a prop to menu: <menu reload={this.state.doSomethingInMenu}>

In the container component, use setTimeout to set the menu variable so control is passed back to the DOM and changes will render (without animation). After setTimeout is ran, the variable will update and the menu props will change, causing it to reload.

In the menu component, check to see if that variable has been updated to the value you're expecting. If it has, call LayoutAnimation.configureNext. This will cause the next rendering (just the menu changes) to be animated.

Code Overview

container component

getInitialState: function() {
  return { doSomethingInMenu: false };
},

// Do something that causes the change
// Then call setTimeout to change the state variable
setTimeout(() => {
  this.setState({ doSomethingInMenu: true });
},750)

<menu reload={this.state.doSomethingInMenu} />

menu component

componentWillReceiveProps: function(nextProps) {
    if (nextProps.reload) {
      LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
      // Set the menu state variable that causes the update
      this.setState({field: value})
    }
  },
like image 54
greg Avatar answered Sep 20 '22 03:09

greg