我有这个组件的问题。我试图将它从 Vanilla JavaScript 转换为完美的。但那不是那么多。我知道我多次使用 setState() 方法有问题,但我无法弄清楚,究竟是什么。有人可以帮我让它充分发挥作用吗?
class AnimationTypingText extends Component {
state = {
text: '',
indexText: 0,
activeLetter: 0,
activeText: 0,
}
idInterval = setInterval(() => this.addLetter(), 50);
addLetter = () => {
const txt = [
'HTML Lorem ipsum dolor sit amet consectetur',
'CSS adipisicing elit. Repellat, laudantium dolorum',
'JavaScript consequuntur dicta esse soluta aut assumenda',
'React error iusto qui atque necessitatibus ',
'Node tempore dolor laborum.',
'MongoDB Aspernatur quam voluptatum quis possimus.'];
if ( this.state.activeLetter >= 0 ) {
this.setState({
text: this.state.text + txt[this.state.activeText][this.state.activeLetter],
});
}
this.setState({
activeLetter: this.state.activeLetter + 1,
});
console.log('check 1')
if ( this.state.activeLetter === txt[this.state.activeText].length ) {
console.log('check 2')
this.setState({
activeText: this.state.activeText + 1,
})
if ( this.state.activeText === txt.length ) return;
return setTimeout(() => {
this.setState({
activeLetter: 0,
text: '',
})
}, 2000)
}
setTimeout(this.idInterval, 100);
}
render() {
return(
<div>
<div onLoad={this.idInterval}>
<p>{this.state.text}</p>
</div>
</div>
)
}
}
ITMISS
相关分类