简体   繁体   English

如何更改reactjs中的状态

[英]How to change the state in reactjs

 import React from 'react'; import ReactDOM from 'react-dom'; import {User} from './components/User'; import {DefaultGame} from './components/DefaultGame'; import {EndGame} from './components/endGame'; class Game extends React.Component { constructor(props) { super(props); this.state= { matchResults:undefined, gameStatus:undefined, };//end if state }//end of constructor startGame(event) { console.log('the game is starting'); this.setState({ gameStatus:true }) } endGameUser(results) { console.log('clicked end Game', results); this.setState({ gameStatus:false, matchResults:'hello' }); console.log(this.state); } render() { if (this.state.gameStatus === true) { console.log('returning this'); return (<User name={prompt('What is your name')} endGame={this.endGameUser.bind(this)} />) } else if (this.state.gameStatus === false) { return ( <EndGame userResultsWins='winnihn' /> ) } else { console.log('returning this not stating') return (<DefaultGame startGameUser={(event) => this.startGame(event)}/>) } } }//end of App component ReactDOM.render(<Game/>, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

 import React from 'react'; import ReactDOM from 'react-dom'; import {User} from './components/User'; import {DefaultGame} from './components/DefaultGame'; import {EndGame} from './components/endGame'; class Game extends React.Component { constructor(props) { super(props); this.state= { matchResults:undefined, gameStatus:undefined, };//end if state }//end of constructor startGame(event) { console.log('the game is starting'); this.setState({ gameStatus:true }) } endGameUser(results) { console.log('clicked end Game', results); this.setState({ gameStatus:false, matchResults:'hello' }); console.log(this.state); } render() { if (this.state.gameStatus === true) { console.log('returning this'); return (<User name={prompt('What is your name')} endGame={this.endGameUser.bind(this)} />) } else if (this.state.gameStatus === false) { return ( <EndGame userResultsWins='winnihn' /> ) } else { console.log('returning this not stating') return (<DefaultGame startGameUser={(event) => this.startGame(event)}/>) } } }//end of App component ReactDOM.render(<Game/>, document.getElementById('app')) 

Hi, I am new to react and i made a simple game of rock paper scissors. 嗨,我是新手做出反应,我做了一个简单的石头剪刀游戏。 I want to change the state of matchResults on the endGameUser function. 我想在endGameUser函数上更改matchResults的状态。 I am able to change the state of gameStatus to false, but i can not change the state of matchResults. 我能够将gameStatus的状态更改为false,但我无法更改matchResults的状态。 Right now i want toi change it to hello, but eventually i want to set it equal to an object. 现在我想把它改成你好,但最终我想把它设置为一个对象。 Can anyone point me in the right direction? 谁能指出我正确的方向?

The state is being changed correctly but the problem is that you're checking the state before it's been updated. 状态正在正确更改,但问题是您在更新之前检查状态。

Try moving your console.log to the callback. 尝试将console.log移动到回调。

this.setState({
    gameStatus:false,
    matchResults:'hello'
}, () => console.log(this.state));

You can use setState . 您可以使用setState Here are the docs ! 这是文档 The way you are currently using setState is actually correct but its not doing what you think it is doing. 您当前使用setState的方式实际上是正确的,但它没有按照您的想法执行。 setState is asynchronous. setState是异步的。 So you can't call setState then right after see that this.state has changed. 因此,在看到this.state已更改后,您无法立即调用setState React batches setState calls until a certain time. React批处理setState调用直到某个时间。 The next time your render is called you will see that the state has changed. 下次调用渲染时,您将看到状态已更改。 Move the console log to render and you will see this. 将控制台日志移动到渲染,您将看到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM