DEV Community

Discussion on: useAxios : A simple custom hook for calling APIs using axios

Collapse
 
jamesburton profile image
James Burton

Other potential enhancements:
Add local storage and stale state cache, to show stale whilst loading new version.
Add withAxios HOC to wrap and inject these properties into any "dumb" component.
Provide an example extending the hook to tailored hooks:
const usePost = id => useAxios('/api/posts/' + id);
... It's an option for grouping your data access into a secondary layer, so components then just hooks the most basic hooks.
Providing examples to disable/mock the fetch in unit tests is a handy one to consider ... Storybook too ... Like having a testData parameter which is used if typeof window != 'object', or jest mocking.
Finally a withAxiosDisplay HOC could wrap that with a loader overlay, loading spinner or skeleton display, error display, and then child component, so you get this:
let Post = ({data}) => {
// Just render from data
};
const Post123 = withAxiosDisplay('/api/post/123', Post):
// Render
... Loading display, error handler, retry and passing data then all included.

Some comments have been hidden by the post's author - find out more