I am working with React Hooks and i am trying to update state then do something async and then do someting based on state. This doesn't work since the update state is not available inside the async function. How could you solve such a task using react hooks?
I basically tried to change the scope of the function but apparently its all immuteable which means the refrences inside the async functions points to old state.
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
// Constants
function somethingAsync(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
function App() {
const [loading, setLoading] = React.useState(false);
const doSomethingAsync = async () => {
setLoading(true);
await somethingAsync(2000);
if (loading) {
setLoading(false);
}
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<p>Current Status: {loading ? "Loading" : "Not Loading"}</p>
<button onClick={doSomethingAsync}>Do Something Async</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
I would like the loading flag to be reset back to false everytime the async function is done and this should be done based on state the have been updated while doing the async function. Right now it only works every second time due to the old references in the async scope.

useEffecthook if I want to execute something based on state changes