Skip to content

Instantly share code, notes, and snippets.

@vuongngo
Last active January 9, 2020 09:35
Show Gist options
  • Save vuongngo/4c078a28638bfb4eb7877fb119b19eda to your computer and use it in GitHub Desktop.
Save vuongngo/4c078a28638bfb4eb7877fb119b19eda to your computer and use it in GitHub Desktop.
const SET_INFO = 'SET_INFO';
const SET_ERROR = 'SET_ERROR';
const initialState = {
info: {},
error: null
};
const reducer = (state, action) => {
switch (action.type) {
case SET_INFO:
return {
...state,
info: action.payload.info
};
case SET_ERROR:
return {
...state,
error: action.payload.error
};
}
};
const useInfo = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const setInfo = (values) => dispatch({
type: SET_INFO,
payload: {
info: values
}
});
const setError = (error) => dispatch({
type: SET_ERROR,
payload: {
error
}
});
return {
...state,
setInfo,
setError,
};
};
// Persist local information
const useLocalInfo = () => {
const { info, setInfo, error, setError } = useInfo();
const { rehydrated } = useSession(info, setInfo);
return {
info,
setInfo,
error,
setError,
rehydrated,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment