I am trying to manage multiple loading states inside a state dynamically with react hooks.
But, when I update the state object depending the result of an api call (success or error), it produces a very odd result.
As in my code example below, a button click will trigger the loading state for the button to be true.
When I click button 1 and then button 2 while button 1 is making a call (here I just simulated with timeout), they both set their loading states to true in order as expected.
Then, after some time, both loading states are set to false.
But, then both call are finished, button 1 loading state sets to true again...!?
I am not sure how I can solve this issue, and your help is appreciated. Thank you.
https://codesandbox.io/s/usestate-async-timeout-issue-oknvu
import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
const [loading, setLoading] = useState({});
const handleOnClick = id => {
setLoading({ ...loading, [id]: true });
setTimeout(() => {
// simulate the case for when error is returned after an api call.
setLoading({ ...loading, [id]: false });
}, 2000);
};
return (
<div className="App">
<h1>React UseState: Updating a state object</h1>
{["1", "2"].map(id => {
return !loading[id] ? (
<button key={id} onClick={() => handleOnClick(id)}>
Item {id}
</button>
) : (
"Loading..."
);
})}
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);