【发布时间】:2019-08-10 05:10:01
【问题描述】:
REACT 新手,问题很简单,有三个“颜色”按钮。单击一个时, h1 标记将更改为按钮颜色。 h1 的默认值为黑色。
const root = document.getElementById("root");
class ChangeColors extends React.Component {
constructor() {
super();
this.state = { color: "black" };
}
render() {
const styleChange = () => {
this.setState({ color: this.state.color })
}
return (
<div>
<h1 style={style}>Change My Colour!</h1>
<p>
<button onClick={this.styleChange} style={color:
red}>Red</button>
<button onClick={this.styleChange} style={color:
blue}>Blue</button>
<button onClick={this.styleChange} style={color:
green}>Green</button>
</p>
</div>
);
}
}
ReactDOM.render(<ChangeColors />, root);
【问题讨论】: