From eaf29a1218604e264593d4579e2fd23307ff59db Mon Sep 17 00:00:00 2001 From: Ward Oosterlijnck Date: Wed, 12 Jun 2019 11:39:11 +1000 Subject: [PATCH] docs: createReducer --- README.md | 1 + docs/createReducer.md | 57 +++++++++++++++++++++++++ src/__stories__/createReducer.story.tsx | 4 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 docs/createReducer.md diff --git a/README.md b/README.md index 7f01d4b8..3349eb9c 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@
- [**State**](./docs/State.md) - [`createMemo`](./docs/createMemo.md) — factory of memoized hooks. + - [`createReducer`](./docs/createReducer.md) — factory of reducer hooks with custom middleware. - [`useGetSet`](./docs/useGetSet.md) — returns state getter `get()` instead of raw state. - [`useGetSetState`](./docs/useGetSetState.md) — as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby. - [`usePrevious`](./docs/usePrevious.md) — returns the previous state or props. diff --git a/docs/createReducer.md b/docs/createReducer.md new file mode 100644 index 00000000..7ad172dc --- /dev/null +++ b/docs/createReducer.md @@ -0,0 +1,57 @@ +# `createReducer` + +Factory for reducer hooks with custom middleware with an identical API as [React's `useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer). Compatible with [Redux middlware](https://redux.js.org/advanced/middleware). + +## Usage + +An example with [`redux-thunk`](https://github.com/reduxjs/redux-thunk) and [`redux-logger`](https://github.com/LogRocket/redux-logger). + +```jsx +import {createReducer} from 'react-use'; +import thunk from 'redux-thunk'; +import logger from 'redux-logger'; + +const useThunkReducer = createReducer(thunk, logger); + +function reducer(state, action) { + switch (action.type) { + case 'increment': + return { count: state.count + 1 }; + case 'decrement': + return { count: state.count - 1 }; + case 'reset': + return init(action.payload); + default: + throw new Error(); + } +} + +const Demo = () => { + const addAndReset = React.useCallback(() => { + return dispatch => { + dispatch({ type: 'increment' }); + + setTimeout(() => { + dispatch({ type: 'reset', payload: 1 }); + }, 1000); + }; + }, []); + + const [count, dispatch] = useThunkReducer(reducer, 1); + + return ( +
+

count: {count}

+ + + +
+ ); +}; +``` + +## Reference + +```js +const useMiddlewareReducer = createReducer(...middlewares); +``` diff --git a/src/__stories__/createReducer.story.tsx b/src/__stories__/createReducer.story.tsx index 65006db7..add19d39 100644 --- a/src/__stories__/createReducer.story.tsx +++ b/src/__stories__/createReducer.story.tsx @@ -4,6 +4,7 @@ import logger from 'redux-logger'; import thunk from 'redux-thunk'; import { createReducer } from '..'; +import ShowDocs from './util/ShowDocs'; const useThunkReducer = createReducer(thunk, logger); @@ -46,10 +47,11 @@ const Demo = ({ initialCount = 1 }) => { +

Open your developer console to see actions logged by middleware

); }; storiesOf('State|createReducer', module) - // .add('Docs', () => ) + .add('Docs', () => ) .add('Demo', () => );