简体   繁体   English

反应 useState 没有立即更新

[英]React useState not updating immediately

I'am new to React and just stumbled upon a problem where useState does not update immediately.我是 React 的新手,只是偶然发现了 useState 不会立即更新的问题。 I've tried answers on stackoverflow but those did not work out because I have multiple useStates that each comprises a list of children components.我已经尝试过有关 stackoverflow 的答案,但没有解决,因为我有多个 useState,每个都包含一个子组件列表。 So using useEffect does not do the trick for me.所以使用 useEffect 对我来说没有用。 The problem for me is that when AIplayCards() is called, useStates aren't changed yet.对我来说,问题是当 AIplayCards() 被调用时, useStates 还没有改变。 So then AIplayCards() executes, the cards are all null.所以然后AIplayCards()执行,卡都是null。

Any help would be appreciated!任何帮助,将不胜感激!

Here is my code snippet:这是我的代码片段:

const Game = (props) => {
    const [mounted, setMounted] = useState(false)
    const [playerCards, setPlayerCards] = useState([])
    const [opponentLeftCards, setOpponentLeftCards] = useState([])
    const [startingTurn,setStartingTurn] = useState(true)
    const [turn, setTurn] = useState(null)

    useEffect(() => {
        resetGame();
        //componentWillMount
        setMounted(true)
    }, []);
    
    useEffect(() => {
        if(mounted){
            if (turn !== "player") AIplayCards()
        }
    }, [mounted]);
    //My effort to make it work

    const resetGame = async () => {
        let deck = Rules.newDeck()
        let playerCards = await Rules.setUserCards(deck)
        let opponentLeftCards = await Rules.setUserCards(deck)
        let firstTurn = Rules.setFirstTurn(playerCards, opponentLeftCards, opponentTopCards, opponentRightCards)
        //Works

        setPlayerCards(playerCards)
        setOpponentLeftCards(opponentLeftCards)
        setTurn(firstTurn)
        setStartingTurn(true)
        //here if I console.log the states and they arent changed yet.
    }

Just for reference purpose:仅供参考:


    const AIplayCards = () => {
        let currentPlayerCards = getCardsforTurn()
        //returns null because turn is still null, and it match with none of the the if statements in getCardsforTurn. 
          ......

When the rest of the code attempts to read the length of currentPlayerCards, an error is returned.当代码的rest尝试读取currentPlayerCards的长度时,返回错误。

You should invoke your second function as a callback to setState, as setState happens asynchronously.您应该调用第二个 function 作为 setState 的回调,因为 setState 是异步发生的。 So setState((state, props) => {...}) instead of setState(object) .所以setState((state, props) => {...})而不是setState(object) If you find that useState / setState are not updating immediately, the answer is simple: they're just queues.如果您发现 useState / setState 没有立即更新,答案很简单:它们只是队列。 React useState and setState don't make changes directly to the state object; React useState 和 setState 不会直接对 state object 进行更改; they create queues to optimize performance, which is why the changes don't update immediately.他们创建队列以优化性能,这就是更改不会立即更新的原因。

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

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