[英]why state is not updated after button click in setinterval react?
Could you please tell me why state is not updated?你能告诉我为什么 state 没有更新吗? On button click I updated the state but when I console my state on setinterval it is not updated why?
在按钮单击时,我更新了 state 但是当我在 setinterval 上控制台我的 state 时它没有更新,为什么?
Here is my code这是我的代码
https://codesandbox.io/s/cool-shamir-8lmpo https://codesandbox.io/s/cool-shamir-8lmpo
<button
onClick={() => {
setState({
filters: {
apps: "DDDDDDH",
searchText: "12333",
taskType: "",
dateFrom: "",
dateTo: "",
status: ""
}
});
}}
>
useEffect(() => {
console.log("===============");
setInterval(() => {
console.log(state);
}, 10000);
}, []);
After button click searchText: "12333",
searchtext should be 12333
in state but it is showing empty wny? After button click searchText: "12333",
搜索文本应该是 state 中的12333
,但它显示为空 wny?
why the value of state in setinterval
is not showing 12333
为什么 setinterval 中
setinterval
的值未显示12333
I think, this is what you want:我想,这就是你想要的:
useEffect(() => {
const interval = setInterval(() => {
console.log(state);
}, 10000);
return () => clearInterval(interval);
}, [state]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.