Skip to content

Instantly share code, notes, and snippets.

@stereobooster
Last active September 17, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereobooster/405983056bc61a7f94c90bacf29f9b95 to your computer and use it in GitHub Desktop.
Save stereobooster/405983056bc61a7f94c90bacf29f9b95 to your computer and use it in GitHub Desktop.
const reducer = (oldState, action) => {
if (typeof action === "function") {
return action(oldState)
} else {
return action;
}
}
// useState is simplified useReducer
const useState = (initialState, init) => {
// it returns [state, dispatch]
// dispatch === setState in this case, because of the way we implemented reducer
return useReducer(reducer, initialState, init);
}
// useCallback is simplified useMemo
const useCallback = (callback, dependencies) => {
return useMemo(() => callback, dependencies);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment