简体   繁体   English

为什么使用钩子单击按钮后 state 不更新?

[英]Why state is not updated after button click using hooks?

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?

What happens here is, The state is changing when the button gets clicked, but to render it in console setInterval should be cleared.这里发生的情况是,state 在单击按钮时会发生变化,但要在控制台中渲染它 setInterval 应该被清除。 try this..尝试这个..

useEffect(() => {
    console.log("===============");
    const interval = setInterval(() => {
      console.log(state);
    }, 10000);
    return () => clearInterval(interval);
}, [state]);

Setting state is async in React, instead of using interval, you need to track the state value useEffect 's dependency array:在 React 中设置 state 是异步的,而不是使用间隔,您需要跟踪 state 值useEffect的依赖数组:

useEffect(() => {
      console.log(state);
  }, [state]);

Why are you using setInterval while you are updating state on button click?为什么在单击按钮时更新 state 时使用 setInterval? Try this.尝试这个。

useEffect(() => {
    console.log("===============");

    console.log(state);
  }, []);

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

相关问题 为什么 state 在 setinterval 中单击按钮后没有更新反应? - why state is not updated after button click in setinterval react? 为什么我的 state 没有使用 React Hooks 更新? - Why is my state not being updated using React Hooks? 无法使用反应钩子传递更新的状态 - Unable to pass updated state using react hooks 使用钩子做出反应时没有得到更新的 state 值? - not getting updated state value in react using hooks? 点击ReactJs后state没有更新 - The state is not updated after click in ReactJs 在 React 中使用 Hooks 设置点击事件的时间间隔不会使用更新的 state 值 - Setting an interval on a click event in React using Hooks won't use updated state values 为什么即使使用钩子更新父 state 也不重新渲染子组件 - Why isn't child component re-rendered even though parent state is updated using hooks 如何在使用反应挂钩的 setState 方法之后访问更新的 state? - How to access the updated state after setState method with react hooks? 单选按钮状态无法在mouseup或单击时更新 - Radio button state not getting updated on mouseup or click 为什么更新的 state 没有反映在事件侦听器中:React Native,Hooks - why updated state not reflected inside an event listener: React Native, Hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM