简体   繁体   English

函数钩子使用中无限循环的问题

[英]problem with infinite loop in function hook useEffect

I have a problem with an infinite loop on my hook, I'm passing the data of the local JSON breakfast. 我的钩子上存在无限循环问题,我正在传递本地JSON早餐的数据。 If you are iterating with map the data and I am taking it to paint a menu of buttons. 如果您要遍历地图数据,那么我将用它来绘制按钮菜单。 If I remove the data at the end of the function, and leave the empty array, it sends me the following error: 如果在函数末尾删除数据,并保留空数组,则会向我发送以下错误:

const BreakfastComponent = () => {

   const breakfast = [
    {
      id: 0,
      name: "Sandwich de jamón y queso",
      price: '35',
      img: "https://i.ibb.co/ynC9xHZ/sandjc.png",

    },
    {
      id: 1,
      name: "Jugo Natural",
      price: '15',
      img: "https://i.ibb.co/8mrd4MK/orangejuice.png",
    },
    {
      id: 2,
      name: "Café americano",
      price: '20',
      img: "https://i.ibb.co/nsj1GL0/americancoffe.png",
    },
    {
      id: 3,
      name: "Café con leche",
      price: '28',
      img: "https://i.ibb.co/GRPBm7j/coffeandmilk.png",
    }
  ];


  const [stateProduct, setStateProduct] = useState([ ]);


  useEffect(() => {

    setStateProduct(breakfast);
  }, [breakfast]);

  return (

      <section className="databreakfast">
        {stateProduct.map((element, item) =>
          <ButtonsBComponent
            key={item}
            {...element}

          />
        )}
        </section>



  )
};

export default BreakfastComponent;

React Hook useEffect has a missing dependency: 'breakfast'. React Hook useEffect缺少依赖项:“ breakfast”。 Either include it or remove the dependency array react-hooks/exhaustive-deps 包括它或删除依赖项数组react-hooks / exhaustive-deps

useEffect 's second argument is an array of state/hooks to be watched and when they change, to run the effect. useEffect的第二个参数是要监视的状态/挂钩的数组,当状态/挂钩更改时,将运行效果。 Since your breakfast is a const, I'm guessing you just want the original stateProduct to be breakfast . 由于您的breakfast是const,所以我猜您只想将原始stateProduct breakfast So instead of using [] as the default, use breakfast . 因此,不要使用[]作为默认值,而应使用breakfast

const [stateProduct, setStateProduct] = useState(breakfast);

Also, probably a good idea to declare const breakfast ... outside of your react functional component so it doesn't re-declare it on every re-render. 另外,在您的react功能组件之外声明const breakfast ...可能是一个好主意,这样它就不会在每次重新渲染时都重新声明它。

The problem is simple, you have breakfast array as a dependency to useEffect and in useEffect you are setting the breakfast array itself. 问题很简单,您将breakfast数组作为对useEffect的依赖useEffect ,在useEffect您要设置Breakfast数组本身。 Now since the const breakfast array is declared inside the component, a new reference to it is generated everytime and since useEffect sets the array in state, it re-renders and on next re-render the comparison for breakfast array fails since the reference has changed. 现在,由于在组件内部声明了const breakfast数组,因此每次都会生成对其的新引用,并且由于useEffect将数组设置为状态,因此它会重新渲染,并且在下一次重新呈现时,因为更改了引用,对breakfast数组的比较将失败。

The solution is simple, you don't need to have the const array in the component and also you don't need to use useEffect 解决方案很简单,您不需要在组件中包含const数组,也不需要使用useEffect

const breakfast = [
    {
      id: 0,
      name: "Sandwich de jamón y queso",
      price: '35',
      img: "https://i.ibb.co/ynC9xHZ/sandjc.png",

    },
    {
      id: 1,
      name: "Jugo Natural",
      price: '15',
      img: "https://i.ibb.co/8mrd4MK/orangejuice.png",
    },
    {
      id: 2,
      name: "Café americano",
      price: '20',
      img: "https://i.ibb.co/nsj1GL0/americancoffe.png",
    },
    {
      id: 3,
      name: "Café con leche",
      price: '28',
      img: "https://i.ibb.co/GRPBm7j/coffeandmilk.png",
    }
  ];

const BreakfastComponent = () => {

  const [stateProduct, setStateProduct] = useState(breakfast);


  return (

      <section className="databreakfast">
        {stateProduct.map((element, item) =>
          <ButtonsBComponent
            key={item}
            {...element}

          />
        )}
        </section>



  )
};

export default BreakfastComponent;

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

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