0

I have some components wrapped in a ScrollView and for some condition the bottom most component shouldn't render. When that happens, I use LayoutAnimation to hide it. The problem is that when the component disappear, the ScrollView jumps to the new content height directly, without any animation at all. I want to use LayoutAnimation since I have screens where the contents height is unknown.

Example image

If you look at the image, when the button is pressed, the screen will instantly jump to the blue box without any animation.

state = { showGreenBox: false };

  renderBottomBox() {
    if (this.state.showGreenBox) {
      return (
        <View style={{ height: 300, width: 100, backgroundColor: 'green' }} />
      );
    }
  }

  render() {
    return (
      <ScrollView>
        <View style={{ height: 300, width: 100, backgroundColor: 'red' }} />
        <View style={{ height: 300, width: 100, backgroundColor: 'blue' }} />
        {this.renderBottomBox()}

        <TouchableOpacity
          onPress={() => {
            LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
            this.setState({ showGreenBox: !this.state.showGreenBox });
          }}
        >
          <Text>Press to collapse green box</Text>
        </TouchableOpacity>
      </ScrollView>
    );
  }
Jaros
  • 41
  • 1
  • 2
  • I copy-paste your code and it gives me perfect animation ... – K.Wu Apr 25 '18 at 01:19
  • 1
    Use 'setLayoutAnimationEnabledExperimental' for android try https://stackoverflow.com/questions/38357368/is-react-natives-layoutanimation-supported-on-android – PTT Apr 25 '18 at 02:50
  • @K.Wu Did you first scroll down to the bottom most of the screen and then pressed the button so that the green box disappeared? Are you on android or iOS? – Jaros Apr 26 '18 at 07:45
  • @PTT I'm using iOS. Should have mentioned. – Jaros Apr 26 '18 at 07:47
  • @Jaros have u found a solution to this ? – Ahmed Eid Apr 22 '20 at 06:54

0 Answers0