简体   繁体   English

为什么 state 在 setinterval 中单击按钮后没有更新反应?

[英]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.

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