代码之家  ›  专栏  ›  技术社区  ›  catandmouse

如何(同时)更改react中状态的多个属性?

  •  0
  • catandmouse  · 技术社区  · 6 年前

    我的主要父组件中有以下状态:

    this.state = {
        playableCards: [],
        openedCard: null,
        offeredChips: 0,
        activePlayer: 0, // first player is 0, second player is 1
        players: [
            {
                name: "player1",
                remainingChips: 11,
                cards: [],
                score: null
            },
            {
                name: "player2",
                remainingChips: 11,
                cards: [],
                score: null
            }
        ]
    };  
    

    takeCard = () => {
        const {
            activePlayer,
            players,
            playableCards,
            offeredChips,
            openedCard
        } = this.state;
    
        if(openedCard) {
    
            // Add card to active player
            let playersClone = [...players];
            playersClone[activePlayer].cards = [
                ...playersClone[activePlayer].cards,
                openedCard
            ];
    
            // Add any offered chips to active player
            playersClone[activePlayer].remainingChips += offeredChips;
    
            this.setState({ players: playersClone }, () =>
                this.calculateScore(activePlayer)
            );
    
            // Remove card from deck
            this.setState({
                playableCards: playableCards.filter(function(card) {
                    return card !== openedCard;
                })
            });
    
            // Change active player
            const nextPlayer = activePlayer === 0 ? 1 : 0;
            this.setState({ activePlayer: nextPlayer });
    
            // Reset offered chips to 0
            this.setState({ offeredChips: 0 });
    
            // Reset opened card
            this.setState({ openedCard: null });
    
        } else {
            console.log("Open a card first!");
        }
    
    };
    

    如您所见,有许多属性仅通过单击事件就可以更改(此方法附加到单击事件)。我想知道这是正确的方法还是我应该把所有的 setState()

    2 回复  |  直到 6 年前
        1
  •  14
  •   Shubham Khatri    6 年前

    render 一次。也就是说,在编写setState时出错的几率很高,以至于批处理忽略了更改或设置了错误的值(例如,您可以基于上一个值为同一个键调用setState两次,并且可能期望得到不同的结果)。因此,建议您在处理所有值之后调用setState一次

        // Add card to active player
        let playersClone = [...players];
        playersClone[activePlayer].cards = [
            ...playersClone[activePlayer].cards,
            openedCard
        ];
    
        // Add any offered chips to active player
        playersClone[activePlayer].remainingChips += offeredChips;
    
        const playableCards = playableCards.filter(function(card) {
                return card !== openedCard;
        })
    
    
        // Change active player
        const nextPlayer = activePlayer === 0 ? 1 : 0;
    
        // Reset offered chips to 0
        // Reset opened card
        // Remove card from deck
        this.setState({ 
              openedCard: null,
              offeredChips: 0, 
              playableCards, 
              players: playersClone
        }, () =>
            this.calculateScore(activePlayer)
        );
    
        2
  •  7
  •   Roopak PutheVeettil    6 年前

    this.setState({ openedCard: null, offeredChips: 0, activePlayer: nextPlayer });
    
        3
  •  0
  •   Nitin Kumar    4 年前

    this.setState({value1: 0, value2: 0})