我正在编写一个自定义钩子来从 API 获取一些数据。如果可能,我希望返回的数据是类型安全的。这可以用泛型来完成吗?
type Action = { type: 'PENDING' } | { type: 'SUCCESS'; payload: any } | { type: 'FAIL' };
interface State {
isLoading: boolean;
isError: boolean;
data: any;
}
const dataFetchReducer = (state: State, action: Action): State => {
switch (action.type) {
case 'PENDING':
return {
...state,
isLoading: true,
};
case 'SUCCESS': {
return {
...state,
isLoading: false,
isError: false,
data: action.payload,
};
}
case 'FAIL':
return {
...state,
isLoading: false,
isError: true,
};
default:
throw new Error('Action not supported');
}
};
const baseUrl = 'http://localhost:4000';
function useDataFetchFromAPI(initUrl: string, initData: any) {
const [url, setUrl] = useState(`${baseUrl}${initUrl}`);
const [state, dispatch] = useReducer(dataFetchReducer, {
isLoading: false,
isError: false,
data: initData,
});
// effect only runs if url changes
// see deps array (2nd argument)
useEffect(() => {
const fetchData = async () => {
dispatch({ type: 'PENDING' });
try {
const result = await axios(url);
dispatch({ type: 'SUCCESS', payload: result.data });
} catch (error) {
dispatch({ type: 'FAIL' });
}
};
fetchData();
}, [url]);
const executeFetch = (url: string) => {
setUrl(url);
};
return { ...state, executeFetch };
}
用法是useDataFetchFromAPI('url', [])
.
我想你可以做类似的事情useDataFetchFromAPI < SomeType > ()
并通过它,但我不确定实现。