1.2.9 • Published 2 years ago

@rest-hooks/use-enhanced-reducer v1.2.9

Weekly downloads
17,452
License
Apache-2.0
Repository
github
Last release
2 years ago

useEnhancedReducer() - middlewares for React Hooks flux stores

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome

useEnhancedReducer() empowers building complex orchestration into flux stores built using React Hooks.

loggerMiddleware.ts

import { MiddlewareAPI, Dispatch } from '@rest-hooks/use-enhanced-reducer';

export default function loggerMiddleware<R extends React.Reducer<any, any>>({
  getState,
  dispatch,
}: MiddlewareAPI<R>) {
  return (next: Dispatch<R>) => async (action: React.ReducerAction<R>) => {
    console.group(action.type);
    console.log('before', getState());
    await next(action);
    console.log('after', getState());
    console.groupEnd();
  };
}

CacheProvider.tsx

import {
  useEnhancedReducer,
  Middleware,
} from '@rest-hooks/use-enhanced-reducer';

interface ProviderProps {
  children: ReactNode;
  middlewares: Middleware[];
  initialState: State<unknown>;
}

export default function CacheProvider({
  children,
  middlewares,
  initialState,
}: ProviderProps) {
  const [state, dispatch] = useEnhancedReducer(
    masterReducer,
    initialState,
    middlewares,
  );

  return (
    <DispatchContext.Provider value={dispatch}>
      <StateContext.Provider value={state}>{children}</StateContext.Provider>
    </DispatchContext.Provider>
  );
}

Middleware Examples

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.2.0-beta.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.5-beta.1

3 years ago

1.1.5-beta.0

3 years ago

1.1.5-beta.2

3 years ago

1.1.2

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

2.0.0-beta.0

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.1-beta.0

5 years ago

0.1.0

5 years ago