简体   繁体   English

Redux Action Creator 导致无限循环

[英]Redux Action Creator Causing Infinite Loop

My application uses the eBay API and allows a user to save a list of favorite items.我的应用程序使用 eBay API 并允许用户保存最喜欢的项目列表。

I'm storing the user's Favorites data which includes the item ID for a product in a user field in my database.我正在存储用户的收藏夹数据,其中包括我数据库中用户字段中产品的项目 ID。

I need to display the saved favorites yet with updated information from the eBay API, since the data saved (such as price, number of bids) is saved statically when a user selects a Favorite.我需要使用来自 eBay API 的更新信息来显示已保存的收藏夹,因为当用户选择收藏夹时,保存的数据(例如价格、出价数量)是静态保存的。

I've set up a separate action creator called getUpdates to the database which makes a call like this, and the call works fine.我已经为数据库设置了一个名为getUpdates的单独动作创建者,它进行这样的调用,并且调用工作正常。

ebayapi.com/itemID=id2342342,id3234234,id82829294,id234234234

In my Favorites component, i begin by making a call to the database to get the user's favorite items.在我的收藏夹组件中,我首先调用数据库以获取用户最喜欢的项目。

useEffect(() => {
    props.getFavorites(props.loggedUser.id);
  }, [props.loggedUser.id]);

This successfully returns and established my favorites state, which includes a list of the user's favorite items.这成功返回并建立了我的favorites状态,其中包括用户最喜欢的项目列表。

I'm trying to take each of the itemId's from the favorites state to pass to my ebay api to retrieve the updated data for each item.我试图将每个 itemId 从收藏夹状态传递给我的 ebay api 以检索每个项目的更新数据。

Now, when I do something like what is shown below with a call to my getUpdates action creator...i'm getting an infinite loop, although it is storing the data properly in a new state field that I've defined favUpdates现在,当我执行如下所示的操作并调用我的getUpdates动作创建器时……我得到了一个无限循环,尽管它正在将数据正确存储在我定义的新状态字段中favUpdates

const Favorites = props => {
  useEffect(() => {
    props.getFavorites(props.loggedUser.id);
  }, [props.loggedUser.id]);

  useEffect(() => {
    setData(props.cardsToShow);
  }, [props]);

  const mapFAVS = props.favorites;
  const data = Array.from(mapFAVS); //the favorites state is an array of objects so transforming data
  const updatedFavs = data.map(item => item.id); //strips out the item id's
  const formatFavs = updatedFavs.map(id => id.join(",")); 
  props.getUpdates(formatFavs);  //updates favUpdates state

I tried to change this so that the getUpdates call was within one of the useEffect hooks, like this...which also causes an infinite loop.我试图改变这一点,以便getUpdates调用在 useEffect 钩子之一内,就像这样......这也会导致无限循环。

const Favorites = props => {
  useEffect(() => {
    props.getFavorites(props.loggedUser.id);
  }, [props.loggedUser.id]);

  useEffect(() => {
    setData(props.cardsToShow);
  }, [props]);

  const mapFAVS = props.favorites;
  const data = Array.from(mapFAVS);
  const updatedFavs = data.map(item => item.id);
  const formatFavs = updatedFavs.map(id => id.join(","));

  useEffect(() => {
    props.getUpdates(formatFavs);
  }, [props]);

If i remove the second props argument the action creator does not get called and there is an axios error on the backend.如果我删除第二个props参数,则不会调用动作创建者,并且后端出现 axios 错误。 Any ideas?有任何想法吗?

This was solved by adjusting the second argument in the useEffect hook.这是通过调整 useEffect 钩子中的第二个参数来解决的。

useEffect(() => {
    props.getUpdates(formatFavs);
  }, [props.favorites]);

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

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