我试图从 react-transition-group 调用特殊的生命周期钩子来使用 gsap 实现动画。但是 TransitionGroup 组件阻止了 Box 组件的渲染,如果我从页面组件中删除了 TransitionGroup 组件,那么 Box 组件就会渲染..请帮我找出问题
import React, { Component } from 'react'
import TransitionGroup from 'react-transition-group/TransitionGroup'
import { TweenMax } from 'gsap'
import './App.css';
class Box extends React.Component {
componentWillEnter (callback) {
const el = this.container;
TweenMax.fromTo(el, 0.3, {y: 100, opacity: 0}, {y: 0, opacity: 1, onComplete: callback});
}
componentWillLeave (callback) {
const el = this.container;
TweenMax.fromTo(el, 0.3, {y: 0, opacity: 1}, {y: -100, opacity: 0, onComplete: callback});
}
render () {
return <div className="box" ref={c => this.container = c}/>;
}
}
export default class Page extends React.Component {
state = {
shouldShowBox: true
};
toggleBox = () => {
this.setState({
shouldShowBox: !this.state.shouldShowBox
})
};
render () {
return (
<div className="page">
<TransitionGroup>
{ this.state.shouldShowBox && <Box/>}
</TransitionGroup>
<button
className="toggle-btn"
onClick={this.toggleBox.bind(this)}
>
toggle
</button>
</div>
);
}
}
我的 package.json 是
"name": "react-with-gsap",
"version": "0.1.0",
"private": true,
"dependencies": {
"classnames": "^2.2.5",
"gsap": "^1.20.3",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.0.17",
"react-transition-group": "^2.2.1",
"victory": "^0.24.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}