9

I'm trying to follow this example (code here) and employ LayoutAnimation inside my RN project (the difference from that example being that I just want to render my circles with no button that'll be pressed).

But when I've added LayoutAnimation, it's the whole view/screen/component that does the animation of 'springing in', not just the circles as I desire. Where do I have to move LayoutAnimation to in order to achieve just the circle objects being animated?

UPDATED AGAIN: Heeded bennygenel's advice to make a separate Circles component and then on Favorites, have a componentDidMount that would add each Cricle component one by one, resulting in individual animation as the state gets updated with a time delay. But I'm still not getting the desired effect of the circles rendering/animating one by one...

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (
        <View>
          { this.props.children }
        </View>
    );
  }
}

class Favorites extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      circleCount: 0
    }
  }
  componentDidMount() {
    for(let i = 0; i <= this.props.screenProps.appstate.length; i++) {
      setTimeout(() => {
        this.addCircle();
      }, (i*500));
    }
  }
  addCircle = () => {
    this.setState((prevState) => ({circleCount: prevState.circleCount + 1}));
  }

render() {
    var favoritesList = this.props.screenProps.appstate;

    circles = favoritesList.map((item) => {
        return (
            <Circle key={item.url} style={styles.testcontainer}>
              <TouchableOpacity onPress={() => {
                  Alert.alert( "Add to cart and checkout?",
                              item.item_name + "? Yum!",
                              [
                                {text: 'Yes', onPress: () => console.log(item.cust_id)},
                                {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
                              ]
                              )}}>
                <Image source={{uri: item.url}} />
               </TouchableOpacity>
            </Circle>
        )});

    return (
        <ScrollView}>
          <View>
            <View>
              {circles}
            </View>
          </View>
        </ScrollView>
    );
  }
}
SpicyClubSauce
  • 4,076
  • 13
  • 37
  • 62

1 Answers1

6

From configureNext() docs;

static configureNext(config, onAnimationDidEnd?)

Schedules an animation to happen on the next layout.

This means you need to configure LayoutAnimation just before the render of the component you want to animate. If you separate your Circle component and set the LayoutAnimation for that component you can animate the circles and nothing else in your layout.

Example

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (<View style={{width: 50, height: 50, backgroundColor: 'red', margin: 10, borderRadius: 25}}/>);
  }
}

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      circleCount: 0
    }
  }
  componentDidMount() {
    for(let i = 0; i < 4; i++) {
      setTimeout(() => {
        this.addCircle();
      }, (i*200));
    }
  }
  addCircle = () => {
    this.setState((prevState) => ({circleCount: prevState.circleCount + 1}));    
  }
  render() {
    var circles = [];
    for (var i = 0; i < this.state.circleCount; i++) {
      circles.push(<Circle />);
    }
    return (
    <View>
      <View style={{flexDirection:'row', justifyContent:'center', alignItems: 'center', marginTop: 100}}>
        { circles }
      </View>
      <Button color="blue" title="Add Circle" onPress={this.addCircle} />
    </View>
    );
  }
}

Update

If you want to use Circle component as your example you need to use it like below so the child components can be rendered too. More detailed explanation can be found here.

class Circle extends Component {
  componentWillMount() {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
  }

  render() {
    return (
        <View>
          { this.props.children }
        </View>
    );
  }
}
bennygenel
  • 23,896
  • 6
  • 65
  • 78
  • hey @bennygenel -- thanks for your response. I've been trying your suggestion albeit in a slightly different implementation -- I'm struggling to get it to work though. I've added it onto the UPDATE section under my original question, perhaps there's a quick thing you see that I am doing incorrectly in trying to implement your suggestion? thanks! – SpicyClubSauce Mar 04 '18 at 19:52
  • thanks Benny, i knew I forgot something basic as passing down those props. The circles do render now as they are supposed to, but the LayoutAnimation still spring the whole view instead of each circle --- basically the whole view/page 'swoops' in, instead of the circles individually doing so. any idea? – SpicyClubSauce Mar 04 '18 at 20:04
  • @SpicyClubSauce I was trying to explain that. LayoutAnimation animate the next render. Since your circles render at the same time they all animate. That is why I gave a delay to the circles in my example and render them one by one. You need to implement similar logic to yours. – bennygenel Mar 04 '18 at 20:13
  • Makes sense @bennygenel -- I made one more (hopefully last) update to the question taking your suggestion. Any ideas on what I'm doing wrong there? – SpicyClubSauce Mar 04 '18 at 20:29
  • @SpicyClubSauce the error is pretty clear. You are trying to set state after your component unmounted because you have a timeout set. you gave a 2 seconds delay and if you have 10 items the 10th item will be added after 20 seconds. – bennygenel Mar 04 '18 at 20:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166205/discussion-between-spicyclubsauce-and-bennygenel). – SpicyClubSauce Mar 04 '18 at 20:53
  • This doesn't work well with react navigation or any other animations - LayoutAnimation still affects all animations globally – evanjmg Nov 06 '18 at 18:23
  • 1
    I'm having the same issue, the answer has been accepted by @SpicyClubSauce were you able to achieve the desired animation? – Romit Kumar Apr 01 '19 at 11:52