[英]getting Too many re-renders. React limits the number of renders to prevent an infinite loop
Learning react by coding, i'm getting this error 'Too many re-renders.通过编码学习反应,我收到此错误“重新渲染太多。 React limits the number of renders to prevent an infinite loop.'
React 限制了渲染的数量以防止无限循环。 this kind of error is usually fixable using useEffect, but because it is in this function that dispatching should happen, i dont know how to put it in useEffect.
这种错误通常可以使用 useEffect 修复,但是因为在这个 function 中应该发生调度,所以我不知道如何将它放入 useEffect 中。
my graph is working fine but when i want to dispatch i get that error.我的图表工作正常,但是当我想调度时,我得到了那个错误。 Simply what i am returning in those functions i want to dispatch it also.
简单地说,我在那些函数中返回的内容我也想分派它。 Any advice?
有什么建议吗? if needed some information ask me.
如果需要一些信息问我。 English is not my mother language so could be mistakes
英语不是我的母语,所以可能是错误的
my code:我的代码:
import { Graph } from "react-d3-graph"; const visualGraph = useSelector((state: ) => state.Articlesgraph ); const filteredGraphData = data && { links: filteredGraphData.links.map((l) => { return { id: l.id, }; }), nodes: filteredGraphData?.nodes.map((n) => { return { id: n.id, }; }), }; const kk = () => { if (currentCam) { dispatch(ChangeGraphData(filteredGraphData)); } return filteredGraphData; }; const pp = () => { if (currentCam) { dispatch(ChangeGraphData(visualGraph)); } return visualGraph; }; <Graph data={filteredGraphData? kk(): pp()} />
You probably don't want to call kk
or pp
every render.您可能不想在每次渲染时都调用
kk
或pp
。 Maybe use a memo?也许使用备忘录?
const hasData = !!filteredGraphData;
const data = React.useMemo(() => hasData ? kk() : pp(), [ hasData ]);
return <Graph data={data} />;
The dependency on hasData
(instead of filteredGraphData
) is hopefully going to break your infinite loop.对
hasData
(而不是filteredGraphData
)的依赖有望打破你的无限循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.