简体   繁体   English

在我的 useEffect 依赖项数组中使用来自上下文提供程序的 state 会导致无限循环

[英]using state from a context provider within my useEffect dependency array causes an infinite loop

I am using the shopify-buy SDK, which allows me to retrieve the current cart of the user.我正在使用shopify-buy SDK,它允许我检索用户的当前购物车。 I am trying to store that cart in my CartProvider which is then used in my Cart component.我正在尝试将该购物车存储在我的CartProvider中,然后在我的Cart组件中使用它。 The problem is when I retrieve information from the cart it's acting a little slow so my component needs to be updated when the state changes, currently I have the following in my getShopifyCart function which is located in my CartProvider .问题是当我从购物车中检索信息时,它的运行速度有点慢,因此当 state 更改时我的组件需要更新,目前我的getShopifyCart function 中有以下内容,它位于我的CartProvider中。

const [cartItems, setCartItems] = useState([])

const getShopifyCart = () => {
    return client.checkout
      .fetch(currentVendor.cartId)
      .then((res) => {
        const lineItemsData = res.lineItems.map((item) => {
          return {
            title: item.title,
            quantity: item.quantity,
          }
        })
        setCartItems(lineItemsData)
        setLoading(false)
      })
      .catch((err) => console.log(err))
  }

In my Cart component I have the following useEffect .在我的Cart组件中,我有以下useEffect

useEffect(() => {
    getShopifyCart()
  }, [cartItems])

But this causes an infinite loop, even though the cartItems state isn't changing.但这会导致无限循环,即使cartItems state 没有改变。

You are setting the state cartItems inside getShopifyCart which you are calling inside a useEffect which has cartItems as a dependency.您正在 getShopifyCart 中设置 state cartItems ,您在getShopifyCart中调用它,它具有useEffect作为依赖cartItems Even though the content of the data has not changed, you are creating a new object, hence its hash has changed as well which causes the useEffect to be called again.即使数据的内容没有改变,您正在创建一个新的 object,因此它的 hash 也发生了变化,这导致再次调用useEffect

If you want to initially fetch the data and set the state, then you need to pass an empty dependency array.如果你想初始获取数据并设置 state,那么你需要传递一个空的依赖数组。

useEffect(() => {
   getShopifyCart()
}, [])

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

相关问题 更新 state 并包含依赖数组时使用 Effect 无限循环 - useEffect infinite loop when updating state and including dependency array 在 useEffect 依赖数组中使用 Redux state 时如何避免无限循环? - How do I avoid infinite loop when using Redux state in useEffect dependency array? 使用依赖数组更新子 useEffect 中的父 state 会导致循环 - Updating parent state in child useEffect with dependency array causes loop 在 useEffect 钩子中添加依赖会导致无限循环 - Adding dependency in useEffect hook causes infinite loop 具有空依赖数组的 useEffect 无限循环 - Infinite loop of useEffect with empty dependency array 具有状态依赖的 useCallback 导致无限循环 - useCallback with state dependency causes infinite loop ReactJS - UseEffect 与上下文一起使用 API 在依赖数组未保持为空时导致无限循环 - ReactJS - UseEffect used with context API is causing infinite loop when dependency array is not kept empty React redux 减速器作为 UseEffect 依赖导致无限循环 - React redux reducer as UseEffect dependency causes infinite loop useEffect 与 function 一起更新 state 作为依赖导致无限循环 - useEffect with function that update the state as dependency leads to infinite loop 当数组作为依赖项传递时,React 中的 useEffect 在无限循环中运行 - useEffect in React runs in an infinite loop, when an array is passed as a dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM