扫码关注官方订阅号
闭关修行中......
代码改改:
class AnimateDemo extends React.Component{ constructor(props) { super(props); this.state = { current: 0 }; } componentDidMount() { this.interval = setInterval(this.tick.bind(this), INTERVAL);//就是这部分出错了,因为this在interval之内变了,需要专门绑定一下 } componentWillUnmount() { clearInterval(this.interval); } tick() { this.setState({current: this.state.current + 1}); } render() { let children = []; let pos = 0; let colors = ['red', 'gray', 'blue']; for (let i = this.state.current; i < this.state.current + colors.length; i++) { let style = { left: pos * 128, background: colors[i % colors.length] }; pos++; children.push(<p key={i} className="animateItem" style={style}>{i}</p>); } return ( <CSSTransitionGroup className="animateExample" transitionEnterTimeout={250} transitionLeaveTimeout={250} transitionName="example"> {children} </CSSTransitionGroup> ); } }
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
代码改改: