简体   繁体   English

谁能解释为什么即使我没有手动设置我的状态也会更新

[英]Can anyone explain why my state is getting updated even when i dont set it manually

So I just spent an hour debugging this code and finally got it to work, but I would want to know why this happened in the first place.所以我只花了一个小时调试这段代码,终于让它工作了,但我想知道为什么会发生这种情况。 I have a function that takes a value from my state, operates on it and saves the output in another variable in the state.我有一个函数,它从我的状态中获取一个值,对其进行操作并将输出保存在状态中的另一个变量中。 This is the fuction:这是功能:

getFolderNames = async () => {
    const promises = this.state.rows.map(async item => {
        if (item[".tag"] == "folder" && item.name.length > 20) {
            item.name = await getFolderName(item.name);
            return item;
        } else return item;
    });
    const result = await Promise.all(promises);
    this.setState({
        rowsToDisplay: result
    });
};

when i run this function, it was updating both the rows and rowsToDisplay to the result variable when i was only calling setState on only one of them.当我运行这个函数时,当我只在其中一个上调用 setState 时,它​​正在将 rows 和 rowsToDisplay 更新为结果变量。

Changing the function as below solves the issue but I would like to know why.更改如下功能可以解决问题,但我想知道原因。

getFolderNames = async () => {
    const promises = this.state.rows.map(async item => {
        if (item[".tag"] == "folder" && item.name.length > 20) {
            let item2 = {
                ...item
            };
            item2.name = await getFolderName(item.name);
            return item2;
        } else return item;
    });
    const result = await Promise.all(promises);
    this.setState({
        rowsToDisplay: result
    });
};

It's because of how JavaScript handles variables.这是因为 JavaScript 处理变量的方式。 When you set a variable to an array or object, it doesn't make a new object but rather just references the original array/object.当您将变量设置为数组或对象时,它不会创建新对象,而只是引用原始数组/对象。

As such, if you set a variable to equal some object, and then set a property of that variable, the original object will also be updated.因此,如果您将变量设置为等于某个对象,然后设置该变量的属性,则原始对象也将被更新。 Check this snippet for an example.检查此代码段以获取示例。

 var foo = {changed: false}; var bar = foo; bar.changed = true; console.log("foo", foo.changed) console.log("bar", bar.changed)

You can read more about the subject here: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0您可以在此处阅读有关该主题的更多信息: https : //codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

I hope this helps you in the future, since I know I also spent many hours banging my head against exactly the sort of cases you described in your original question.我希望这在将来对您有所帮助,因为我知道我也花了很多时间来解决您在原始问题中描述的那种情况。

暂无
暂无

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

相关问题 谁能解释为什么我收到 map function 错误 - can anyone explain why i'm getting a map function error 为什么我的 state 即使在编写 setState 方法后也没有更新? - Why my state is not getting updated even after writing the setState method? 我不知道为什么我的组件不呈现更新状态。 即使状态被更新并反映在数据库中 - I can't figure out why my component does not rendered with the updated state. Even though, the state is updated and reflected in the database 任何人都知道为什么在尝试设置加载状态时为什么会不断收到此错误 - AnyOne know why i keep getting This error when i try to set state on load 我不想在 select 标签中设置第一个选项的 state 怎么做任何人都可以帮助我 - i dont want set the state of first option in select tag how to do that can anyone help me ReactJS-谁能解释这种状态的改变方式和原因? - ReactJS - Can anyone explain how and why this state is being altered? 当我在 state 中放入道具时,即使更新了道具,state 也不会更新 - When I put props in state, state is not updated even if props is updated 谁能解释为什么我收到有关“相同密钥”的错误 - Can anyone explain why I'm getting the error about "same key" 为什么即使默认情况下将状态设置为空数组也无法定义状态? - Why my state is underfined even when I've set it as an empty array by default? 使用 React Js 创建一个简单的 todolist。不知道为什么我的状态没有用新的 todo 项目更新 - Creating a simple todolist using React Js.Dont know why my state is not getting updated with new todo Item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM