简体   繁体   中英

How to change button text in Reactjs for a few seconds?

I know how to change the text of button (using a component state for example). But how to change the text for a few seconds?

For example: I click button with the text "Save". The request goes on the server and when I get response from the server and there are no mistakes, I need to change the text of the button on "Saved" for 3 seconds and return "Save" then.

you can do it like so

handleClick = () => {
    this.setState({ text: "Loading..." });
    setTimeout(() => {
        this.setState({ text: "Save" });
    }, 5000);
};

Change the state of button text inside timeout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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