简体   繁体   English

React state object 中的变异值

[英]Mutating value in a React state object

I feel really stupid for asking such a simple question like this, but for some reason I forgot how to change the value in a React state object.问这样一个简单的问题我觉得很愚蠢,但由于某种原因,我忘记了如何更改 React state object 中的值。 For example:例如:

const initialState = {
   likes: 100,
   dislikes: 25,
   isLiked: false,
   isDisliked: false
}

const [data, setData] = useState(initialState)

const handleLikes = e => {
    if (data.isLiked) {
        setData({ ...data, likes: data.likes - 1 });
    } else {
        setData({ ...data, likes: data.likes + 1 });
    }

    setData({ ...data, isLiked: !data.isLiked })
}

<button className={`like-button ${data.isLiked ? 'liked' : ''}`} onClick={handleLikes}>Like | <span className='likes-counter'>{data.likes}</span></button>

The confusing part, the part that made me ask this question, is the likes: data.likes + 1 part.令人困惑的部分,让我问这个问题的部分,是喜欢:data.likes + 1部分。 When I click the button, it adds/removes the className as intended, but the value never changes.当我单击按钮时,它会按预期添加/删除 className,但值永远不会改变。 I know this is such a simple thing but I have spent a while on this.我知道这是一件很简单的事情,但我已经花了一段时间。

What is wrong with the likes: data.likes + 1 ?喜欢有什么问题: data.likes + 1

The two subsequent setData calls end up "overwriting" each other since the data captured by your handleLikes will not have changed after you call setData .随后的两个setData调用最终会相互“覆盖”,因为在您调用setData后,您的handleLikes捕获的data不会发生变化。

You'll want to use the functional form of setState to be able to reliably look at the current value (even when asynchronous modifications are in queue, then return a new value based on it:您将希望使用setState的函数形式能够可靠地查看当前值(即使异步修改在队列中,然后基于它返回一个新值:

const handleToggleLikes = (e) => {
  setData((data) =>
    data.isLiked
      ? { ...data, likes: data.likes - 1 }
      : { ...data, likes: data.likes + 1 },
  );
  setData((data) => ({ ...data, isLiked: !data.isLiked }));
};

You can do both of these modifications in one invocation:您可以在一次调用中完成这两项修改:

const handleToggleLike = e => {
    setData(data => ({
        ...data,
        likes: data.likes + (data.isLiked ? -1 : 1),  // decrease if previously liked
        isLiked: !data.isLiked,
    }));
}

There are two ways to use a setState function to update the state object:有两种方法可以使用 setState function 来更新 state object:

  1. Pass it the new state object (which is what you are doing in your code)将新的 state object 传递给它(这是您在代码中所做的)
  2. Pass it a callback function that takes the current state object as argument, uses it to create the new state object, and returns the new state object. Pass it a callback function that takes the current state object as argument, uses it to create the new state object, and returns the new state object.

Whenever you want to use the current state object to create a new state object, you must use the second way.每当你想使用当前的 state object 来创建新的 state ZA8CFDE6331BD59EB2666F891ZB 时,你必须使用第二种方式。 In your case, your code:在您的情况下,您的代码:

setData({...data, likes: data.likes - 1})

uses the current state object (data) to create the new state object.使用当前的 state object(数据)创建新的 state ZA8CFDE6331BD59EB2AC966F8911C46。 So you must use a callback instead, like this:因此,您必须改用回调,如下所示:

setData((cur_data)=>({...cur_data, likes: cur_data.likes-1}))

Note that I am using the arrow syntax in the callback function to return the new state object.请注意,我在回调 function 中使用箭头语法来返回新的 state object。

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

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