【问题标题】:How to make a transition when using conditional rendering in React在 React 中使用条件渲染时如何进行转换
【发布时间】:2018-07-30 02:34:43
【问题描述】:

问题详情:

  • 原生环境
  • 我无法使用display:none
  • 我无法使用transition:all
  • 我只能通过js的方法使用animate

我的渲染函数(不是 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>
    );
  }

当我点击组件Touchable时,它会改变this.state.show,并有条件地渲染组件&lt;Text&gt;

如何使用我的动画功能,如anmate(){...}实现转场?

我是这样弄的:(没用)

pressHandle(){
  this.setState({
    show:!this.state.show
  });
  this.transitionHandle();
}
transitionHandle(){
  animate(this.refs.box,{
    opacity:1
    // ....
  })
}

【问题讨论】:

  • 能否提供animate()函数是如何实现的?除了 React Native Animated API,我从未听说过 React Native 中的任何纯 JS 动画解决方案。
  • @ThoVu NOT ReactNative,animate(dom,{},function(){ }),如animate(findDOMNode("#id"),{ opacity:0 },() =&gt; { console.log("end") });

标签: javascript reactjs transition


【解决方案1】:

您需要保持它在 DOM 中呈现,以便您可以有条件地对其应用 CSS。

改变这个:

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

收件人:

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

要么更改引用的 CSS 样式,要么添加/删除类名并完全在 CSS 中控制过渡。

【讨论】:

  • 看问题详情,我只能用animate(){ }这个功能,我不能用transition:all time,也不能用tramsorm:xxxx
猜你喜欢
  • 2020-09-20
  • 2021-07-18
  • 2019-08-18
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 2018-10-02
相关资源
最近更新 更多