简体   繁体   English

错误:重新渲染过多。 React 限制渲染次数以防止无限循环。 反应

[英]Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. React

I am Facing the Below Mentioned Error When Fetching Data from Firebase and Setting it to State Inside a useEffect Hook.从 Firebase 获取数据并将其设置为useEffect Hook 内的状态时,我useEffectuseEffect

Here is The Code i am Using With Simplified Markup:这是我使用简化标记的代码:

function PostComponent({ id }){
  const [post,setPost] = useState({});
  const [error,setError] = useState("");

  const db = firebase.firestore();

  useEffect(()=>{
    db.collection('posts').doc(id).get().then(doc=>{
      setPost(doc.data());
    }).catch(err=>setError(err.message));
  }, []);

  return (
    {error && <h1>{ error }</h1>}
    <PostCard data={post} />
  );
}

Update!!更新!!

  • I Fixed The Error Because i realised that it was an issue with Setting the state inside the child component.Thanks to everybody that invested time into Helping Me.我修复了错误因为我意识到这是在子组件中设置状态的问题。感谢所有投入时间帮助我的人。

Can you pls try it like this:你能试试这样吗:


//this can be outsite of your component
const db = firebase.firestore();

function PostComponent({ id }){
  const [post,setPost] = useState({});
  const [error,setError] = useState("");

  useEffect(()=>{
    db.collection('posts').doc(id).get().then(doc=>{
      setPost(doc.data());
    }).catch(err=>setError(err.message));
  }, [id]);

  return (
    {error && <h1>{ error }</h1>}
    <PostCard data={post} />
  );
}

If that doesn't work then the error is probably comming from another component.如果这不起作用,则错误可能来自另一个组件。

暂无
暂无

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

相关问题 错误:重新渲染过多。 React 限制了渲染的数量以防止无限循环。 - 反应 - Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. - React 错误:重新渲染过多。 React 限制了渲染的数量以防止无限循环。 - 反应 JS - Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. - React JS “未捕获的错误:重新渲染过多。React 限制渲染次数以防止无限循环。” - "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop." 错误:重新渲染过多。 React 限制了渲染的数量以防止无限循环。 内部使用效果 - Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. Inside useEffect 太多的重新渲染。 React 限制了渲染的数量以防止无限循环。 反应钩和 ReactJS - Too many re-renders. React limits the number of renders to prevent an infinite loop. React Hook and ReactJS 太多的重新渲染。 React 限制了渲染的数量以防止无限循环。 使用状态问题? - Too many re-renders. React limits the number of renders to prevent an infinite loop. useState problem? 太多的重新渲染。 React 限制渲染次数以防止无限循环。? - Too many re-renders. React limits the number of renders to prevent an infinite loop.? 太多的重新渲染。 react 限制渲染的数量以防止无限循环。 使用效果 - too many re-renders. react limits the number of renders to prevent an infinite loop. UseEffect 带有钩子的 React Query 抛出错误,“未捕获的错误:重新渲染太多。React 限制渲染次数以防止无限循环。” - React Query with hooks is throwing error, "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop." React.js “错误:重新渲染太多。 React 限制了渲染的数量以防止无限循环。” - React.js “Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM