简体   繁体   English

React 和道具深度的问题

[英]Issue with React and depth of props

I don't know why this is happening.我不知道为什么会这样。 When I try to setArray to a data.map, React complains and I have no idea why.当我尝试将 setArray 设置为 data.map 时,React 抱怨,我不知道为什么。 Here are the codes of key components.以下是关键组件的代码。 I've also tried to move the useState hook into the 's parent element and passing it as props but I got the same error我也尝试将 useState 钩子移动到 的父元素中并将其作为道具传递,但我得到了同样的错误在此处输入图像描述

My Data Component:我的数据组件:

export let DataContext = createContext();

export let DataProvider = (props) => {
  let [array, setArray] = useState([]);
  let [data, setData] = useState([
    {
      name: "Toshiba",
      desc: "Laptop copmuter",
      price: 299,
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "Toshiba",
      type: "laptop",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space",
    },
    {
      name: "Lenovo",
      desc: "Laptop copmuter",
      price: 399,
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "Lenovo",
      type: "laptop",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this spaceSome words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space",
    },
    {
      name: "Asus",
      desc: "Laptop copmuter",
      price: 199,
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "Asus",
      type: "laptop",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space",
    },
    {
      name: "HP",
      desc: "Laptop copmuter",
      price: 299,
      type: "laptop",
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "HP",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space  Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space ",
    },
    {
      name: "Apple",
      desc: "Laptop copmuter",
      price: 299,
      type: "laptop",
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "Apple",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this spaceSome words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space",
    },
    {
      name: "Apple",
      desc: "Android",
      price: 299,
      type: "mobile",
      url:
        "https://images.pexels.com/photos/3975677/pexels-photo-3975677.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
      path: "Apple",
      details:
        "Some words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this spaceSome words just to fill this space Some words just to fill this space Some words just to fill this space Some words just to fill this space",
    },
  ]);

  return (
    <DataContext.Provider value={[data, setData, array, setArray]}>
      {props.children}
    </DataContext.Provider>
  );

****My Item Component that I get error on: **** ****我收到错误的项目组件:****

function Item() {
  let [data, setData, array, setArray] = useContext(DataContext);

  let newAr = data.map((item) => {
    return (
      <div className="Item">
        <img src={item.url} />
        <h1>{item.name}</h1>
        <p>{item.desc}</p>
        <h2>{item.price}</h2>
        <Link to={`/product/${item.path}`}>
          <h3>See details</h3>
        </Link>
      </div>
    );
  });

  setArray([newAr]);

  return <>{array}</>;
}

You should not do你不应该做


  setArray([newAr]);

inside the component as it is.在组件内部。 It needs to be done or in event handler, or in useEffect hook.它需要在事件处理程序中或在 useEffect 挂钩中完成。

Right now, you just recursively updating component.现在,您只需递归更新组件。 Component renders - component updates newAr - it gets signal set newAr is updated - it rerender components - component updates newAr - etc It falls in a never ending loop.组件渲染 - 组件更新 newAr - 它获取信号集 newAr 被更新 - 它重新渲染组件 - 组件更新 newAr - 等等 它陷入一个永无止境的循环。

try尝试

  useEffect(() => {
    setArray([newAr]);
  }, []) 

This will update newAr only when component is mounted.这将仅在安装组件时更新 newAr。

PS It is no a good approach to store React Nodes in the state. PS 在 state 中存储 React 节点不是一个好方法。

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

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