简体   繁体   English

你能解释一下这个反应 function 吗?

[英]Can you explain this react function?

This is the function that fetches the products from the database for the products page of a eCommerce website.这是 function 从数据库中获取电子商务网站产品页面的产品。

I am trying to write a similar function for sign-in.我正在尝试编写类似的 function 用于登录。

Can you help me with that I am new to react?你能帮我解决一下我是新来的反应吗?

storedProducts = () => {
  axios.get('http://127.0.0.1:5000/product')
    .then(res => {
      const storedProducts = res.data;
      let tempProducts = [];
      storedProducts.forEach(item => {
        const singleItem = { ...item
        };
        tempProducts = [...tempProducts, singleItem];

      });
      this.setState(() => {
        return {
          products: tempProducts
        };
      });
    })
}

In each iteration the current item of the storedProduct is cloned into a new object ( singeItem ).在每次迭代中, storedProduct的当前项被克隆到一个新的 object ( singeItem ) 中。 In the next line this singleItem is appended to a new array that is reassigend to tempProducts .在下一行中,这个singleItem被附加到一个重新分配给tempProducts的新数组中。 tempProducts is a complete new array object created using the spread operator. tempProducts是使用扩展运算符创建的全新数组 object。

The code you pasted is an assignment of an anonymous arrow function to a variable storedProducts .您粘贴的代码是将匿名箭头 function 分配给变量storedProducts

It seems to be cut from a class definition of a component.它似乎是从组件的 class 定义中删除的。 Components are the building blocks of React javascript library that allow building UI by building trees of these components, similar to HTML.组件是 React javascript 库的构建块,它允许通过构建这些组件的树来构建 UI,类似于 HTML。

The only part that deals with anything related to React is this one:处理与 React 相关的任何事情的唯一部分是这个:

this.setState(()=>{
  return {products:tempProducts};
});

It sets the state of the component, which normally causes the component to rerender – https://reactjs.org/docs/state-and-lifecycle.html它设置组件的 state,这通常会导致组件重新渲染 – https://reactjs.org/docs/state-and-lifecycle.html

const storedProducts = res.data;
let tempProducts = [];
storedProducts.forEach(item => {
  const singleItem = { ...item };
 tempProducts = [...tempProducts, singleItem];
});

This whole block is quite strange, because seems unneeded.这整个块很奇怪,因为似乎不需要。 It takes an array res.data (I know it's an array because forEach is used to iterate it in the following line, and that is an Array method) and iterates over items of it, creating copies using spread syntax const singleItem = {...item };它需要一个数组res.data (我知道它是一个数组,因为forEach用于在下一行中迭代它,这是一个 Array 方法)并迭代它的项目,使用spread语法const singleItem = {...item }; (literally, assign new object to singleItem and copy all properties of item into it). (字面意思是,将新的 object 分配给 singleItem 并将item的所有属性复制到其中)。 Since res.data is already an array, it could be used directly to set state of the component like this:由于res.data已经是一个数组,因此可以直接使用它来设置组件的 state,如下所示:

this.setState(()=>{
  return { products: res.data };
});

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

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