I've got the following React function (using the alpha of React Hooks) that has a TypeScript error (thought it still works). The error is pointing to the line of code where the axios promise completes with .then(a=>{setData(a))... It is complaint about a I think.
TS2345: Argument of type 'void | AxiosResponse' is not assignable to parameter of type 'SetStateAction'. Type 'void' is not assignable to type 'SetStateAction'.
Here is a link to the code: https://codesandbox.io/s/7zyx4vv6k6
Is there more that should be typed in this file? I'm new to TypeScript and was suprised how little I needed to do to make it only complain about this one thing. In codesandbox it does not show me the tpyescript error that I can see. Is there a way to make it show up there like it does in my webstorm IDE? In codesandbox, the line I want to make type correct is 32 even though it does not show it in codesandbox as an error
import { useState, useEffect } from "react";
import axios from "axios";
const useAxiosFetch = (url: string, timeout: number) => {
const [data, setData] = useState(null);
const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
let unmounted = false;
let source = axios.CancelToken.source();
axios
.get(url, {
cancelToken: source.token,
timeout: timeout,
})
.catch(function(e) {
if (!unmounted) {
setError(true);
setErrorMessage(e.message);
setLoading(false);
if (axios.isCancel(e)) {
console.log(`request cancelled:${e.message}`);
} else {
console.log("another error happened:" + e.message);
}
}
})
.then(a => {
if (!unmounted) {
setData(a);
setLoading(false);
}
});
return function() {
unmounted = true;
source.cancel("Cancelling in cleanup");
};
}, []);
return { data, loading, error, errorMessage };
};
export default useAxiosFetch;