Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a transition when using conditional rendering in React

Question details:

  • native environment
  • I couldn't use display:none
  • I couldn't use transition:all
  • I can only use animate through a method of js

My render function (not react-native) :

render() { 
    return (
      <View className="container">
        <Touchable onPress={this.pressHandle}>Click</Touchable>
        <View ref="box">
          {this.state.show ? <Text className="show">show Text</Text> : null}
        </View>
      </View>
    );
  }

when I click the component Touchable, it whill change this.state.show,and conditional render the component <Text>

How can I use my animate function ,such as anmate(){...} to achieve transition?

I did it like this:(it didn't work)

pressHandle(){
  this.setState({
    show:!this.state.show
  });
  this.transitionHandle();
}
transitionHandle(){
  animate(this.refs.box,{
    opacity:1
    // ....
  })
}
like image 362
Alen Avatar asked Jul 21 '26 19:07

Alen


1 Answers

You need to keep it rendered in the DOM so that you can apply CSS to it conditionally.

Change this:

{this.state.show ? <Text className="show">show Text</Text> : null}

To:

<Text className="show">show Text</Text>

And either change the CSS styles of your ref or add/remove a class name and control the transition completely in CSS.

like image 187
Scott Avatar answered Jul 24 '26 09:07

Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!