我正在学习如何做出一个简单的游戏。在这个游戏中,我有一个容器组件来管理倒计时。当玩家开始游戏时,我运行以下功能:
startTimer = () => {
setInterval(() => {
this.setState((prevState) => ({
timeLeft:prevState.timeLeft-1
}))
},1000)
};
我的容器组件具有以下呈现方法:
render() {
return (
<div className="container">
<GameStats
gameStarted={this.state.gameStarted}
question={this.state.question}
numOfQuestions={this.state.numOfQuestions}
correctAnswers={this.state.correctAnswers}
timeLeft={this.state.timeLeft}
/>
<Board
gameStarted={this.state.gameStarted}
startGame={this.startGame}
squareClicked={this.squareClickedHandler}
sqOne={this.state.squareOne}
sqTwo={this.state.squareTwo}
sqThree={this.state.squareThree}
sqFour={this.state.squareFour}
/>
</div>
)
}
gamestats组件负责渲染剩余时间,而board组件不使用剩余时间。因此,将Board组件转换为基于类的组件并实现ShouldComponentUpdate挂钩,这样它就不会每秒重新发送一次了,这是否有意义?