-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createReducer for redux middleware, related #164
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import logger from 'redux-logger'; | ||
import thunk from 'redux-thunk'; | ||
|
||
import { createReducer } from '..'; | ||
// import ShowDocs from './util/ShowDocs'; | ||
|
||
const useThunkReducer = createReducer(thunk, logger); | ||
|
||
// React useReducer lazy initialization example: https://reactjs.org/docs/hooks-reference.html#lazy-initialization | ||
function init(initialCount) { | ||
return { count: initialCount }; | ||
} | ||
|
||
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 = ({ initialCount = 1 }) => { | ||
// Action creator to increment count, wait a second and then reset | ||
const addAndReset = React.useCallback(() => { | ||
return dispatch => { | ||
dispatch({ type: 'increment' }); | ||
|
||
setTimeout(() => { | ||
dispatch({ type: 'reset', payload: initialCount }); | ||
}, 1000); | ||
}; | ||
}, [initialCount]); | ||
|
||
const [state, dispatch] = useThunkReducer(reducer, initialCount, init); | ||
|
||
return ( | ||
<div> | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
<button onClick={() => dispatch(addAndReset())}>Add and reset</button> | ||
<button onClick={() => dispatch({ type: 'reset', payload: initialCount })}>Reset</button> | ||
<button onClick={() => dispatch({ type: 'increment' })}>+</button> | ||
<button onClick={() => dispatch({ type: 'decrement' })}>-</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('State|createReducer', module) | ||
// .add('Docs', () => <ShowDocs md={require('../../docs/createMemo.md')} />) | ||
.add('Demo', () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useReducer } from 'react'; | ||
|
||
function compose(...funcs) { | ||
if (funcs.length === 0) { | ||
return arg => arg; | ||
} | ||
|
||
if (funcs.length === 1) { | ||
return funcs[0]; | ||
} | ||
|
||
return funcs.reduce((a, b) => (...args) => a(b(...args))); | ||
} | ||
|
||
const createReducer = (...middlewares) => (...args) => { | ||
const [state, dispatch] = useReducer(...args); | ||
|
||
let middlewareDispatch = () => { | ||
throw new Error( | ||
'Dispatching while constructing your middleware is not allowed. ' + | ||
'Other middleware would not be applied to this dispatch.' | ||
); | ||
}; | ||
|
||
const middlewareAPI = { | ||
getState: () => state, | ||
dispatch: (...args) => middlewareDispatch(...args), | ||
}; | ||
const chain = middlewares.map(middleware => middleware(middlewareAPI)); | ||
middlewareDispatch = compose(...chain)(dispatch); | ||
|
||
return [state, middlewareDispatch]; | ||
}; | ||
|
||
export default createReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters