Skip to content

Instantly share code, notes, and snippets.

@dnshko
Created August 2, 2020 13:28
Show Gist options
  • Save dnshko/9a8855eba48c781bc1a11ad46e731ece to your computer and use it in GitHub Desktop.
Save dnshko/9a8855eba48c781bc1a11ad46e731ece to your computer and use it in GitHub Desktop.
useReducer
const initialState = {
status: 'idle',
error: null,
data: [],
};
const [state, dispatch] = useReducer((state, action) => {
switch (action.type) {
case 'FETCHING':
return { ...initialState, status: 'fetching' };
case 'FETCHED':
return { ...initialState, status: 'fetched', data: action.payload };
case 'FETCH_ERROR':
return { ...initialState, status: 'error', error: action.payload };
default:
return state;
}
}, initialState);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment