cuzfrog / useEffectReducer

Simply Perform Effects in Reducer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simply Perform Effect in Reducer

This is not library/dependency. It's a simple pattern you copy and go.

Why

The simple pattern:

type UseEffectReducer<S, A> = [
  state: S,
  dispatch: (action: A) => void,
];

export function useEffectReducer<S, A>(reducer: (state: S, action: A) => S, initialState: S): UseEffectReducer<S, A> {
  const [state, setState] = useState<S>(initialState);
  const dispatch = (action: A) => {
    setState(reducer(state, action));
  };
  return [state, dispatch];
}

function myReducer(...) {
  // perform effects as needed
}

Of course, can have different signatures of reducer:

function myReducer(state: S, action: A, doSomething: () => void): S {
  //...
  doSomething();
  //...
}

Caveats

  • dispatch should be called from events, not effects.
  • Errors properly handled so they don't fail the reducer.

More Implemetations:

License

By Cause Chung (cuzfrog@gmail.com).

CC0 1.0. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/

About

Simply Perform Effects in Reducer


Languages

Language:TypeScript 96.9%Language:Shell 3.1%