简体   繁体   English

使用对象作为参数的无限循环 useEffect [React_Native]

[英]Infinite loop useEffect using objects as a parameter [React_Native]

  const [ state, setState] = useState({
casas: [],
casaSelecionada: '',
 });




  useEffect(()=>{
    dbref.once('value', function(snapshot, prevChildKey){
      setState({
        casas:(Object.entries(snapshot.val())).map( e => e[1].casaItem),
      });
    });
   dbref.off('value'); // according to the firebase website itself,  
                       //this function serves to remove all callbacks 
                      // from one location.
  },[]);

I started using the hooks, and I came across an infinite loop problem when trying to set my state, I already looked in several forums and all possible solutions did not solve my problem.我开始使用钩子,在尝试设置我的 state 时遇到了无限循环问题,我已经查看了几个论坛,所有可能的解决方案都没有解决我的问题。 I happen to be trying to set an array of objects, and when I send update, it says that it does not recognize "state.casas" as an object, but when I pass "state.casas" as the second parameter in useEffect inside "[]", it still in the loop.我碰巧试图设置一个对象数组,当我发送更新时,它说它不能将“state.casas”识别为 object,但是当我将“state.casas”作为 useEffect 里面的第二个参数传递时“[]”,它仍然在循环中。 Can anybody help me?有谁能够帮我?

You are getting an infinite loop because state.casas is endlessly being updated.你得到一个无限循环,因为state.casas正在无休止地更新。 Each time the handler for the firebase on triggers which updates state.casas , it causes the hook to execute.每次更新state.casas on触发器上的 firebase 的处理程序时,它都会导致挂钩执行。 In addition to that, every time the hook executes, it creates new firebase subscriptions that run in addition to the other existing subscriptions with each updating casas constantly causing more executions of the hook.除此之外,每次钩子执行时,它都会创建新的 firebase 订阅,这些订阅与其他现有订阅一起运行,每次更新casas都会不断导致更多的钩子执行。 Your useEffect should probably only ever be run once as the single firebase subscription is at most what you'd need for that ref to get realtime updates.您的useEffect应该只运行一次,因为单个 firebase 订阅最多是您获得实时更新所需的那个参考。 Also moving the handler to a function will make it easier to cleanup the subscription for when the component unmounts:还将处理程序移动到 function 将更容易在组件卸载时清理订阅:

const [state, setState] = useState({
  casas: [],
  casaSelecionada: '',
});

useEffect(() => {
  // Move logic to a shared handler for easier `on`/`off`
  function handleValueChange(snapshot, prevChildKey) {
    setState(prevState => {
      return {
        ...prevState,
        casas: (Object.entries(snapshot.val())).map( e => e[1].casaItem),
      };    
    });
  }

  dbref.on('value', handleValueChange);

  // Return a function that you can unsubscribe from firebase to avoid leaks
  return () => {
    dbref.off('value', handleValueChange);
  }
}, []);

Notice a cleanup function was return at the end of the hook containing the off .请注意清理 function 在包含off的钩子末尾返回。 This is to remove the subscription when the component unmounts.这是在组件卸载时删除订阅。 Otherwise you could have leaks.否则你可能会有泄漏。 Notice that is a returned function.请注意,这是一个返回的 function。 In your example you are simply calling off rather than returning a function which is what theguide on hooks indicates.在您的示例中,您只是off而不是返回 function ,这是挂钩指南所指示的。 If you decide to use once , then you probably don't need a cleanup function, but once may not be enough if you need constant realtime updates.如果您决定使用once ,那么您可能不需要清理 function,但如果您需要不断的实时更新, once可能还不够。

Hopefully that helps!希望这会有所帮助!

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

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