这个简单的事情应该很容易完成,但是我要把它弄复杂。
我要做的就是动画化React组件的安装和卸载。到目前为止,这是我尝试过的方法,以及每种解决方案都不起作用的原因:
ReactCSSTransitionGroup
ReactTransitionGroup
TransitionMotion
left: -10000px
我想要一些 易于 实现的东西。在安装时,为一组样式设置动画;卸载时,为一组相同(或另一组)样式设置动画。做完了 它还必须在多个平台上都具有高性能。
我在这里撞墙了。如果我缺少某些东西,并且有一种简单的方法可以做到,请告诉我。
这有点冗长,但是我使用了所有本机事件和方法来实现此动画。否ReactCSSTransitionGroup,ReactTransitionGroup等等。
我用过的东西
onTransitionEnd
如何运作
mounted
opacity: 0
componentDidMount
componentWillReceiveProps
opacity: 1
继续循环。
查看代码,您会明白的。如果需要任何澄清,请发表评论。
希望这可以帮助。
class App extends React.Component{ constructor(props) { super(props) this.transitionEnd = this.transitionEnd.bind(this) this.mountStyle = this.mountStyle.bind(this) this.unMountStyle = this.unMountStyle.bind(this) this.state ={ //base css show: true, style :{ fontSize: 60, opacity: 0, transition: 'all 2s ease', } } } componentWillReceiveProps(newProps) { // check for the mounted props if(!newProps.mounted) return this.unMountStyle() // call outro animation when mounted prop is false this.setState({ // remount the node when the mounted prop is true show: true }) setTimeout(this.mountStyle, 10) // call the into animation } unMountStyle() { // css for unmount animation this.setState({ style: { fontSize: 60, opacity: 0, transition: 'all 1s ease', } }) } mountStyle() { // css for mount animation this.setState({ style: { fontSize: 60, opacity: 1, transition: 'all 1s ease', } }) } componentDidMount(){ setTimeout(this.mountStyle, 10) // call the into animation } transitionEnd(){ if(!this.props.mounted){ // remove the node on transition end when the mounted prop is false this.setState({ show: false }) } } render() { return this.state.show && <h1 style={this.state.style} onTransitionEnd={this.transitionEnd}>Hello</h1> } } class Parent extends React.Component{ constructor(props){ super(props) this.buttonClick = this.buttonClick.bind(this) this.state = { showChild: true, } } buttonClick(){ this.setState({ showChild: !this.state.showChild }) } render(){ return <div> <App onTransitionEnd={this.transitionEnd} mounted={this.state.showChild}/> <button onClick={this.buttonClick}>{this.state.showChild ? 'Unmount': 'Mount'}</button> </div> } } ReactDOM.render(<Parent />, document.getElementById('app')) <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-with-addons.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>