简体   繁体   English

React Hooks - useState 不会立即更新“正在保存...”、“已保存”状态

[英]React Hooks - useState does not update “Saving…”, “Saved” states immediately

I'm using React Hooks and I'm creating a page such that users can make changes to their account and save it.我正在使用 React Hooks 并且我正在创建一个页面,以便用户可以更改他们的帐户并保存它。 When the page loads, I want the button to be disabled because the page will show the previously saved information and there's no need to save since no changes were made.当页面加载时,我希望该按钮被禁用,因为该页面将显示之前保存的信息,并且由于没有进行任何更改而无需保存。 Once the user makes a change, I have a validate() function that will check if whatever changes they made are valid.一旦用户进行了更改,我就会有一个 validate() 函数来检查他们所做的任何更改是否有效。 If they are then, the button should be enabled.如果是,则应启用该按钮。 When the user clicks the save button, the button should be disabled and "Saving" should show.当用户单击保存按钮时,该按钮应被禁用并显示“正在保存”。 Once it's done saving, the button should still be disabled and "Saved" should show.完成保存后,该按钮仍应处于禁用状态,并应显示“已保存”。

I have 3 state variables that are initialized like so (disableButton is true at first because the button is disabled on landing the page).我有 3 个像这样初始化的状态变量(disableButton 一开始为真,因为该按钮在登陆页面时被禁用)。

const [saved, setSaved] = useState(false);
const [isSubmitting, setSubmitting] = useState(false);
const [disableButton, setDisableButton] = useState(true);

I used setState when certain events occur, for example, when the user clicks the save button:我在某些事件发生时使用了 setState,例如,当用户单击保存按钮时:

const handleSubmit = async () => {
    setSaved(false);
    setSubmitting(true);
    setDisableButton(true);
...
}

I noticed that these changes aren't made immediately, but the next time it renders, which is a problem for me.我注意到这些更改不会立即进行,而是在下次渲染时进行,这对我来说是个问题。 I looked into useEffect as well, but I'm not sure if it will work in my case (or maybe I'm just not seeing a pattern).我也研究了 useEffect ,但我不确定它是否适用于我的情况(或者我只是没有看到模式)。 I'm new to React so any help would be appreciated.我是 React 的新手,所以任何帮助将不胜感激。

All state updates in React are asynchronous by design as to try and batch events together instead of triggering a render per change. React 中的所有状态更新在设计上都是异步的,因为它们一起尝试和批处理事件,而不是每次更改都触发渲染。

Class based component have a way to React to state updates via a setState callback:基于类的组件可以通过setState回调对状态更新做出反应:

onEvent = () => {
    this.setState({ key: value }, () => {
        console.log("I'm run when the state has been updated!");
    }); 
}

For function components, the principle still applies although in a different way:对于功能组件,该原则仍然适用,但方式不同:

export const SomeComponent = () => {
    const [value, setValue] = useState(false);

    useEffect(() => {
        console.log("I'm run whenever value changes!");
    }, [value]);

    function onEvent() {
        setValue(!value);
    }

    // ...
}

That's how this is designed so you will have to find a way to make it work for your use case.这就是它的设计方式,因此您必须找到一种方法使其适用于您的用例。 Feel free to add more detail about your particular situation.随意添加有关您的特定情况的更多详细信息。

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

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