【发布时间】:2019-11-14 19:53:20
【问题描述】:
我正在处理一项任务,我需要每 1 秒更改一次反应代码中句子或段落中每个单词的颜色。
这是我的代码:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
colors: ["red", "yellow", "blue", "green", "purple", "pink"]
};
this.changeBg = this.changeBg.bind(this);
}
componentDidMount() {
setInterval(this.changeBg, 1000);
}
changeBg() {
const { colors } = this.state;
const color = colors[Math.floor(Math.random() * colors.length)];
document.body.style.backgroundColor = color;
}
render() {
return (
<div>
This is a sample message in react template
</div>
);
}
}
export default App;
这可以很好地改变内容的背景。但我的任务是每 1 秒随机更改每个单词的颜色。
文本示例这是反应模板中的示例消息
This 应该有一种颜色,is 应该有另一种颜色,同样所有的单词。我怎样才能做到这一点?
现在这些颜色应该每 1 秒再次变化一次。
【问题讨论】:
标签: reactjs