[英]How do I fix React rendering issues?
I have a very simple component that fetches data and do some easy computation.我有一个非常简单的组件,它可以获取数据并进行一些简单的计算。 Every time the page refreshes the component renders, the value will change.
每次页面刷新组件渲染时,值都会改变。 I have also tried clearing the cache thinking it might be an issue but it didn't solve the problem.
我也尝试过清除缓存,认为这可能是一个问题,但并没有解决问题。 Thank you for your help!
谢谢您的帮助!
I have tried removing and I get a warning with a message suggesting to add difference
but even without it, it still behaves the same on every single render.我已经尝试删除并收到一条警告,其中包含一条建议添加
difference
的消息,但即使没有它,它在每个渲染中的行为仍然相同。 I use difference
value like this我使用这样
difference
值
const [revenue, setRevenue] = useState([]);
const [difference, setDifference] = useState(0);
useEffect(() => {
const getRevenue = async () => {
try {
const res = await adminRequest.get("/orders/incomestats");
setRevenue(res.data);
console.log(res.data);
setDifference((res.data[1].total * 100) / (res.data[0].total - 100));
console.log(difference);
} catch (error) {
console.log(error);
}
};
getRevenue();
}, [difference]);
Here is where I use the value difference
这是我使用价值
difference
的地方
%{Math.floor(difference)}
{difference < 0 ? (
<ArrowDownward />
) : (
<ArrowUpward />
)}
Each state change generates the component rendering.每个 state 更改都会生成组件渲染。 You are listening to the "difference" variable change and changing it again with setDifference, creating a loop.
您正在收听“差异”变量更改并使用 setDifference 再次更改它,从而创建一个循环。
This这个
useEffect(()=>{
setState(something)
},[state])
Use利用
useEffect(()=>{
setState(something)
},[])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.