Last active
January 9, 2020 09:35
-
-
Save vuongngo/4c078a28638bfb4eb7877fb119b19eda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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