reactjs - 反应状态没有从不同的调用中更新
问题描述
在使用反应的记忆游戏项目中,我试图在游戏结束时使用一个按钮创建弹出窗口,引导您进入下一个级别(更多卡片)。为了创建下一个级别,我使用了初始化状态的 resetGame 函数。属性“isFinished”影响弹出窗口是否显示。
如果我按下重置按钮 - 没有问题。如果我按下下一级按钮 - 状态中的所有属性都会初始化,除了 isFinished 并且弹出窗口仍然存在。如您所见,resetGame 是从重置按钮和 FinishPopup 组件中调用的。
render() {
return (
<div style={boardStyle}>
<h1 style={titleStyle}>Memory Game</h1>
<Timer reset={this.state.resetRequest} />
<div>
<Button onClick={()=>this.resetGame(rowsVar,columnsVar)} type="button" className="btn btn-secondary">Reset</Button>
</div>
<div style={cardWrapper}>
{this.renderBoard()}
{this.checkIfGameOver()}
{this.state.isFinished ? (<FinishPopup nextLevel={()=>this.resetGame(rowsVar+1,columnsVar+1)} />) : null }
</div>
</div>
);
}
这是 resetGame 函数:
resetGame=(rows,columns)=>{
rowsVar=rows;
columnsVar=columns;
let newState = Object.assign({},this.state);
newState.board = this.initialBoard(rows,columns);
newState.numOfOpenCars = 0;
newState.lastId="";
newState.isFinished=false; //this is the property that does not changing
newState.matchCounter=0;
this.setState(newState);
}
这是弹出渲染功能:
render() {
return (
<div style={popupStyle}>
<div>
<h1>
Well done!!
</h1>
<button onClick={this.props.nextLevel}>Next Level</button>
</div>
</div>);
}
我究竟做错了什么?
解决方案
推荐阅读
- outlook - 修改了重复约会中单个实例/异常的描述,在 Outlook 中没有反映?
- android - 从 Play 商店获取拒绝消息“您的应用容易受到 Intent 重定向”
- angular - matDatepickerFilter 打破 mat-datepicker
- java - how to call a method with multiple threads
- php - 在 Drupal 8 的文本字段中集成选择下拉菜单
- debugging - 如何使用汇编语言打印“Hello World”
- c++ - 如何将我创建的自定义容器放入库中,以便可以像任何其他容器一样初始化它(例如 std::vector )?
- node.js - nodemailer 部署到天蓝色时的不稳定行为
- python - 我一次又一次地收到这个错误(TypeError:不能将序列乘以非“列表”类型的整数)
- jenkins - 使用詹金斯在运行时调用变量时出错