let INTERVAL = 2000;
class AnimateDemo extends React.Component{
constructor(props) {
super(props);
this.state = {
current: 0
};
}
componentDidMount() {
this.interval = setInterval(this.tick, 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(<div key={i} className="animateItem" style={style}>{i}</div>);
}
return (
<CSSTransitionGroup
className="animateExample"
transitionEnterTimeout={250}
transitionLeaveTimeout={250}
transitionName="example">
{children}
</CSSTransitionGroup>
);
}
}
出错信息是
Uncaught TypeError: Cannot read property 'current' of undefined
代码改改: