简体   繁体   English

如何更改处于状态的对象的属性?

[英]How do I change the properties of an object in state?

I have been working with the React framework for a couple weeks now, but I am surprised by new things very often. 我已经使用React框架工作了两个星期,但是我经常为新事物感到惊讶。 I am working on the login/game lobby/ server lobby of a game I am making for a group project. 我正在为团体项目制作游戏的登录/游戏大厅/服务器大厅。

I am a bit confused about state currently. 我对目前的状态有些困惑。 So far to change my state I have been using: 到目前为止,要更改我的状态,我一直在使用:

setState({stateProperty: newValue});

The reason I am confused is that I have an array in state that contains a list of elements. 我感到困惑的原因是我有一个状态数组,其中包含一个元素列表。 Each element is a line displayed on the screen, and I need to change one of words in this element. 每个元素都是屏幕上显示的一行,我需要更改此元素中的单词之一。 The element needs to be seen by all viewers of the site, so these lines must be kept in state. 该元素必须由网站的所有查看者看到,因此这些行必须保持在状态中。

The problem is that this word is a property of this element. 问题在于该词是该元素的属性。 To be able to set the state I need to pass a new version of this element, but I cannot do this because I would need to make a version of this element with a different property... I cannot change properties. 为了能够设置状态,我需要传递此元素的新版本,但是我不能这样做,因为我需要使用不同的属性来制作此元素的版本...我无法更改属性。

It seems I have to change the state of the state or something? 看来我必须更改状态或其他状态? How would I go about doing this? 我将如何去做呢?

The properties of the array get pushed in such a way, where I am trying to change "status" of the "GameLine" object, and "GameLine" is one of the elements in the array "children": 数组的属性以这种方式推送,其中我试图更改“ GameLine”对象的“状态”,而“ GameLine”是数组“ children”中的元素之一:

children.push(
  <GameLine 
    players={allPlayers} 
    lobbyStateChange={this.lobbyStateChange.bind(this)}
    joinState={this.joinState.bind(this)} 
    joinAppears={this.joinAppears.bind(this)}
    handleLobbyChange={this.handleLobbyChange.bind(this)} 
    lobby={newLobbyName} 
    owner={owner} 
    type={newGameType} 
    difficulty={newDifficulty} 
    status={newStatus} 
    number={gamesAmount}
    key={"gameLineKey"+owner} 
    className="gamesGames"
  />
);

Sorry if this is a bad question! 抱歉,这是一个不好的问题! Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

Severiaan's answer is exactly what I was looking for! Severiaan的答案正是我想要的! Thanks so much! 非常感谢!

To reiterate what my question was, for future readers: 重申一下我的问题,对于以后的读者:

Here is the initial state of my program: 这是我程序的初始状态:

    this.state = {
        inLogin: true,
        inGameLobby: false,
        inLobbyCreate: false,
        currentGame: 0,
        message: "",
        allMessages: [],
        messageAmount: 0,
        games: 0,
        allGameLines: [],
        allGames: [],
        lobbyName: "",
        owner: "",
        gameType: "",
        difficulty: "",
        status: "",
        user: "",
        allUsers: [],
        userAmount: 0,
        selectedGame: "",
        buttonVisibility: "hidden",
        username: "",
        role: "",
        currentTab: "logTab",
        allLobbies: [],
        allLobbyPlayers: [],
        playersReady: [],
        allGameLobbyMessages: [],
    }

On my webpage, every time someone creates a 'game lobby', a 'gameLine' is created that other players can click so to be able to join the lobby. 在我的网页上,每次有人创建“游戏大厅”时,都会创建一个“ gameLine”,其他玩家可以单击该行以加入该大厅。 The 'gameLine' displays the amount of players in the lobby as 'status'. “ gameLine”将大厅中的玩家数量显示为“状态”。 I was uncertain how to change this property of the 'gameLine'. 我不确定如何更改“ gameLine”的此属性。

The state property 'allGameLines' is a list of the 'gameLine' element, where the element (is complicated and is unnecessary to display all the code for, not needed to understand the question) is going to be re-rendered when someone joins or leaves the lobby. 状态属性“ allGameLines”是“ gameLine”元素的列表,当有人加入或加入该元素时,该元素(很复杂,不需要显示所有代码,不需要了解问题)将被重新呈现。离开大厅。

The issue was that the 'gameLine' element has its own properties. 问题是'gameLine'元素具有自己的属性。 Normally when editing state, you would use the command: 通常,在编辑状态时,可以使用以下命令:

    setState({
            allGameLines: newAllGameLines,
    });

'setState' cannot be used in this case because we cannot create a new version 'newAllGameLines' because to create a new version we would need to change the property 'status'. 在这种情况下,不能使用'setState',因为我们不能创建新版本'newAllGameLines',因为要创建新版本,我们需要更改属性'status'。

"Lukasz 'seVeriaan' Grela" has the perfect solution of going into 'allGameLines' and changing 'status' directly. “ Lukasz'seVeriaan'Grela”拥有进入“ allGameLines”并直接更改“状态”的完美解决方案。 The solution would look something like: 解决方案如下所示:

    lobbyStateChange(newGameStatus, allLobbyPlayers, jTerm){
        this.setState(prevState =>({
            inGameLobby: true,
            buttonVisibility: "hidden",
            allLobbyPlayers,
            allGameLines: {
                ...prevState.allGameLines,
                jTerm: {
                    props: {
                        status: newGameStatus,
                    }
                 }
             },
        }));
    }

Thank you all for the help! 谢谢大家的帮助!

You can change state granularly you have to copy unmodified state, eg 您可以细粒度地更改状态,您必须复制未修改的状态,例如

this.state ={ prop1:”abc”,
prop2:{
    Sub1:”abc1”,
    Sub2:”def2”
}:

this.setState(prevState=>({
    prop2:{
        ...prevState.prop2,
        Sub2:”changed”
    }
});

This will change sub2 only. 这只会更改sub2。

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

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