简体   繁体   English

函数组件内的 React 非状态变量不会立即更新

[英]React non state variables inside function component are not updated instantly

I know that useState hook of react is asynchronous so if I use useState to store variable and call set function on it, it may not be updated instantly.我知道 React 的 useState 钩子是异步的,所以如果我使用 useState 来存储变量并在其上调用 set 函数,它可能不会立即更新。

However now I am using simple variables to store the value, but still the variable value is not updated.但是现在我使用简单的变量来存储值,但变量值仍然没有更新。 How can I resolve the same?我该如何解决?

const List=(props)=>{
    let count = 1;
    const onNextButtonClick = ()=>{
        count  = count +1;
        console.log(count );
        updatePage();
    }
    return (
        //html
    )
}

I see that the value of c is not getting incremented whenever the next button is clicked and I am getting the same value of c on console.我看到每当单击下一个按钮时 c 的值都没有增加,并且我在控制台上获得了相同的 c 值。

Why is this happening?为什么会这样?

count needs to be stored in state and, because state updates may be grouped and processed asynchronously, you won't see the change until the next render which is where useEffect comes in useful. count需要存储在 state 中,并且因为 state 更新可能会被分组和异步处理,所以在下一次渲染之前你不会看到变化,这就是useEffect有用的地方。

 const { useEffect, useState } = React; function Example() { const [ count, setCount ] = useState(0); function onNextButtonClick() { setCount(count + 1); // the other thing } useEffect(() => { if (count) console.log(count); }, [count]); return ( <div> <p>You clicked {count} times</p> <button onClick={onNextButtonClick}> Click me </button> </div> ); }; // Render it ReactDOM.render( <Example />, document.getElementById("react") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

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

相关问题 反应 function 组件的 setState 未更新我的 state - setState for react function component not updated my state 重新渲染太多 | 反应应用 | 组件功能中更新的状态变量不起作用 - Too many re-renderds | React App | State Variables updated in Component Function Not Working 如何从 redux 存储在反应组件中的正常 function 中获取更新 state - How to get updated state from redux store inside a normal function in react component React:useEffect 中更新的非状态变量显示旧值 - React: Updated non-state variables in useEffect show old value React Function Component Form没有设置状态变量? - React Function Component Form not setting state variables? React:组件函数内部未定义&#39;this.state&#39; - React: 'this.state' is undefined inside a component function 在 React 功能组件内的函数中传递状态 - Passing state in function inside React functional component 在非React文件中访问React组件的状态 - Access React component's state inside a non React file 在 React 中使用 useState 未在函数组件中更新状态 - State is not getting updated in Function Component using useState in React 在componentDidMount()上传递要更新的参数的函数内部的设置状态做出反应 - React setting state inside function on componentDidMount() passing parameters to be updated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM