【发布时间】:2019-01-15 03:47:12
【问题描述】:
警报模式对话框即使状态改变也不会改变
我分享了 codepen here。 这是组件。
class RecordingReports extends React.Component {
constructor(props) {
super(props);
this.state = {
dialog: null,
newContent: {
contentType: "recording"
}
};
}
toggleContentType = () => {
let newContent = this.state.newContent;
this.setState({
newContent: {
...newContent,
contentType:
newContent.contentType == "recording" ? "report" : "recording"
}
});
};
showDialog = () => {
this.setState({
dialog: (
<div>
<button
onClick={() => {
this.toggleContentType();
}}
>
{this.state.newContent.contentType} // it remains recording
</button>
</div>
)
});
};
render() {
console.log(this.state.newContent); //this shows state changes
return (
<div>
{this.state.dialog}
<button onClick={() => this.showDialog()} >show Toggle Button</button>
</div>
);
}
}
要测试第一次单击显示切换,您会看到标题为 recording 的按钮,我希望它在单击时切换,但它永远不会。
我分享了 codepen here。
如果你看到控制台,你会看到它改变了状态。
我不确定是什么问题,
谢谢
【问题讨论】:
-
OP 你能检查你的codepen链接吗?它不包含您显示的示例代码。
-
codepen 显示一些其他代码
-
对不起,我更新了,请重新审核。
标签: javascript reactjs state