I have the following code. When pressing the button, the expected behavior is that the AxiosInstance is recreated and the state is updated. But in reality, the instance is recreated and then executed somewhere.
function createApi(): AxiosInstance {
const api = axios.create();
// ...
return api;
}
function App() {
const [api, setApi] = React.useState<AxiosInstance>(() => createApi());
const resetApi = () => {
setApi(createApi());
};
console.log(api);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button onClick={resetApi}>Reset API</button>
</div>
);
}
There is no error in Typescript, setApi has the following signature in my IDE:
React.Dispatch<React.SetStateAction<AxiosInstance>>
Which will be evaluated to something like this:
(value: AxiosInstance) => void
Which is exactly what I did in the first code snippet, so why does it not work?