reactjs - 与 REACT (setState()) 斗争
问题描述
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);
解决方案
您的代码中有很多语法错误,但这里有一个固定版本:https ://codesandbox.io/s/62row8358z
推荐阅读
- algorithm - 2n 和 m 代表什么?
- android - 如何使用 android 获取 Firebase 数据库中的值?
- c++ - 如何在不知道最初输入多少值的情况下将整数存储到数组中
- amazon-web-services - 使用 AWS Lambda 批量处理文件的超时问题
- python - 我得到 ValueError: 'Dip Time' 不在列表中
- android - WorkManager 和 Kodein
- microservices - 身份验证策略的 Istio 替代方案是什么,开发的 Istio 流程是什么?
- linkedin - LinkedIn - 无法创建访问令牌,回复“此应用程序不允许创建应用程序令牌”。#linkedin
- php - 邮件函数返回真但没有收到电子邮件
- c# - 构建 IQueryable
然后将其附加到 DbSet