简体   繁体   English

React.js组件中的无限控制台日志记录

[英]Infinite console logging in React.js component

I'm working on my MERN app, and when I'm logging smth in NewsPage component, it logs infinitely. 我正在使用自己的MERN应用,当我在NewsPage组件中记录smth时,它会无限记录。

NewsPage component: NewsPage组件:

const NewsPage = ({news, fetchNews}) => {
  const postNews = (title, body) => {
    axios.post("http://localhost:9000/news", { title, body });
  }

  useEffect(() => {
    fetchNews();

  }, [fetchNews, postNews])

  return (
    <>
      <AddNewsForm postNews={postNews}/>
      <h1>News:</h1>
      <NewsItemPage news={news} />
    </>
  );
};

const mapStateToProps = state => ({
  news: state.news
})

export default connect(mapStateToProps, {fetchNews})(NewsPage);

Fetch news action: 获取新闻操作:

export const fetchNews = () => dispatch => {
  fetchRequest();

  try {
    const fetch = async () => {
      const res = await axios.get("http://localhost:9000/news");

      dispatch({
        type: a.FETCH_NEWS_SUCCESS,
        payload: res.data
      });
    }
    fetch()
  } catch (e) {
    dispatch({
      type: a.FETCH_NEWS_FAILURE,
      error: e
    });
  }
}

It works correctly, I can fetch news from and post news to my backend, but if I log anything in console, it would be logging infinitely, and I will not get any error. 它可以正常工作,我可以从中获取新闻,也可以将新闻发布到我的后端,但是如果我在控制台中记录任何内容,它将无限地记录日志,并且不会出现任何错误。

is there a way to fix this, and is this a real problem? 有没有办法解决这个问题,这是一个真正的问题吗?

Its likely because whatever function the console log is located in is being used in render , which itself is a loop. 可能是因为控制台日志所位于的任何功能都在render中使用,而render本身是一个循环。 Otherwise, there is no other way that I can see why it would repeat. 否则,我没有其他办法可以知道为什么会重复。 It probably won't end up being a problem, unless the code you are executing slows down, which might cause performance issues in the future. 除非您正在执行的代码变慢,否则可能最终不会成为问题,否则将来可能会导致性能问题。

You're tracking fetchNews and postNews in your useEffect array, which will re-rerun fetchNews(); 您正在useEffect数组中跟踪fetchNewspostNews ,这将重新运行fetchNews(); on every render. 在每个渲染上。

Unless the values in the useEffect second argument are primitives, you need to use some deep compare methods for those: https://stackoverflow.com/a/54096391/4468021 除非useEffect第二个参数中的值是基元,否则您需要对它们使用一些深层比较方法: https : useEffect

Actually, you have wrong useEffect usage. 实际上,您使用了错误的useEffect用法。

Effect would be called each time when component receive new props, so, it looks like this: 每当组件接收到新的道具时,都会调用效果,因此,它看起来像这样:

1) Component mounts, call function from useEffect 1)组件安装,从useEffect调用函数
2) It makes API call, update data in store 2)进行API调用,更新存储中的数据
3) Data passed to container, updates "dumb" component 3)将数据传递到容器,更新“哑巴”组件
4) Dumb component makes re-rendering, calling func from useEffect , and that's infinity loop. 4)Dumb组件进行重新渲染,从useEffect调用func,这就是无限循环。

In fact, It is pretty weird that you don't have memory leak. 实际上,您没有内存泄漏是很奇怪的。
What you can do, is: 您可以做的是:

1) Add some conditional rendering. 1)添加一些条件渲染。 I pretty sure, you need to call it only on initial load. 我很确定,您只需要在初始加载时调用它即可。
2) Add something like ImmutableJS , it would not re-render component and would not mutate store if nothing has changed 2)添加类似ImmutableJS东西,如果没有任何变化,它将不会重新渲染组件,也不会使存储发生变化

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

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