【发布时间】:2018-12-21 01:31:29
【问题描述】:
我尝试通过单击一个按钮来随机更改整个页面的背景颜色,但它只是更改了 div 元素的背景。这是我的代码
import React from "react";
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
quotes: ["red", "yellow", "blue", "green", "purple", "pink"],
colors: [],
};
this.nextColor = this.nextColor.bind(this);
}
// moves to display next quotes
nextColor() {
const random = this.state.quotes[Math.floor(Math.random() * this.state.quotes.length)]
console.log("receiving ", random);
this.setState({
colors: random
})
}
render() {
return (
<div style={{backgroundColor: this.state.colors}}>
<h1>{this.state.colors}</h1>
<button onClick={this.nextColor}>Next Color</button>
</div>
)
}
}
ReactDOM.render(<Home />, document.getElementById("app"));
单击 nextColor 按钮时,我需要更改整个页面的背景颜色。您的帮助将不胜感激
【问题讨论】:
标签: javascript reactjs