简体   繁体   English

在 React、类组件中更新状态主体时遇到问题

[英]Having trouble with updating the state's body in react, class components

I haven't worked with react in a while and I'm having some trouble giving the tableData's body property the result of the fetched data.我有一段时间没有使用 react 了,我在为 tableData 的 body 属性提供获取数据的结果时遇到了一些麻烦。 The fetched data is simply an object that contains the fetched values of the tableData's selectors.获取的数据只是一个包含 tableData 选择器的获取值的对象。 How can I do this?我怎样才能做到这一点?

const fetchData = async () => {
  const result = await authAxios.get(apiUrl);
  return result.data.data;
};

class Index extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: {
        header: [
          {
            name: "Account Type",
            selector: "accountType",
            sortable: true
          },
          {
            name: "Email",
            selector: "email",
            sortable: true
          },
          {
            name: "ID",
            selector: "id",
            sortable: true
          },
          {
            name: "Phone Number",
            selector: "phoneNumber",
            sortable: true
          },
          {
            name: "Username",
            selector: "userName",
            sortable: true
          }
        ],

        body: []
      }
    };
  }

  componentDidMount() {
    setTimeout(function () {
      index();
    }, 800);
  }
  render(){
  ...
  }
  }

You could simply update the state by copying over all the previous values to be as it is and updating only the body property.您可以通过将所有先前的值按原样复制并仅更新body属性来简单地更新状态。 One way to accomplish this would be to use the spread operator ... .实现此目的的一种方法是使用扩展运算符...
You could fetch the result and update the state in your componentDidMount .您可以获取结果并更新componentDidMount的状态。

async componentDidMount() {
    setTimeout(function () {
      index();
    }, 800);

   const res = await fetchData()
   var stateProperty = {...this.state.tableData}
   stateProperty.body = [res]; //Your object would then be stored in body as [{...}]
   this.setState({tableData:someProperty},()=>console.log(this.state.tableData)) //Second argument is a callback to verify if the state has been updated or not, have put up there just to verify, you are free to remove it.
  }

So, we're basically copying over the entire state of tableData to stateProperty then mutating the body property to store the result from fetch , you might be tempted to mutate the state directly cutting the copying part, but that would not give you the desired results, because you are not supposed to mutate the state directly mainly because they are immutable in nature, and also mutating the state directly would trigger no re-renders which would then contradict the whole concept of React.所以,我们基本上的整个状态复制在tableDatastateProperty然后变异的body属性来存储结果,从fetch ,你可能会变异状态直接切割复制的一部分,但不会给你想要的结果,因为你不应该直接改变状态,主要是因为它们本质上是不可变的,而且直接改变状态不会触发重新渲染,这会与 React 的整个概念相矛盾。 Hence, we are making the necessary changes to the copied over variable then assigning that variable to the state altogether.因此,我们正在对复制的变量进行必要的更改,然后将该变量完全分配给状态。

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

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