Skip to content

Instantly share code, notes, and snippets.

@stereobooster
Last active September 17, 2020 13:03
Show Gist options
  • Select an option

  • Save stereobooster/405983056bc61a7f94c90bacf29f9b95 to your computer and use it in GitHub Desktop.

Select an option

Save stereobooster/405983056bc61a7f94c90bacf29f9b95 to your computer and use it in GitHub Desktop.

Revisions

  1. stereobooster revised this gist Sep 17, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions simplied.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ const reducer = (oldState, 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);
    }
  2. stereobooster revised this gist Sep 17, 2020. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions simplied.js
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,8 @@ const reducer = (oldState, action) => {

    // useState is simplified useReducer
    const useState = (initialState, init) => {
    const [state, dispatch] = useReducer(reducer, initialState, init);
    const setState = dispatch;
    return [state, setState];
    // dispatch === setState in this case, because of the way we implemented reducer
    return useReducer(reducer, initialState, init);
    }

    // useCallback is simplified useMemo
  3. stereobooster revised this gist Sep 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simplied.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ const reducer = (oldState, action) => {
    // useState is simplified useReducer
    const useState = (initialState, init) => {
    const [state, dispatch] = useReducer(reducer, initialState, init);
    const setState = useCallback((newState) => dispatch(newState), [dispatch])
    const setState = dispatch;
    return [state, setState];
    }

  4. stereobooster created this gist Sep 17, 2020.
    19 changes: 19 additions & 0 deletions simplied.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    const reducer = (oldState, action) => {
    if (typeof action === "function") {
    return action(oldState)
    } else {
    return action;
    }
    }

    // useState is simplified useReducer
    const useState = (initialState, init) => {
    const [state, dispatch] = useReducer(reducer, initialState, init);
    const setState = useCallback((newState) => dispatch(newState), [dispatch])
    return [state, setState];
    }

    // useCallback is simplified useMemo
    const useCallback = (callback, dependencies) => {
    return useMemo(() => callback, dependencies);
    }