-
-
Save james-gardner/e52e26f6fb196526047cf1ecd43da3de to your computer and use it in GitHub Desktop.
React Hooks: useReducer with actions and selectors (Redux-like)
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
function useSelectors(reducer, mapStateToSelectors) { | |
const [state] = reducer; | |
const selectors = useMemo(() => mapStateToSelectors(state), [state]); | |
return selectors; | |
} | |
function useActions(reducer, mapDispatchToActions) { | |
const [, dispatch] = reducer; | |
const actions = useMemo(() => mapDispatchToActions(dispatch), [dispatch]); | |
return actions; | |
} | |
const initialState = { count: 0 }; | |
function reducer(state = initialState, action) { | |
switch(action.type) { | |
case 'increment': | |
case 'decrement': | |
return { count: state.count + action.amount }; | |
} | |
} | |
function Example() { | |
const counterReducer = useReducer(reducer, initialState); | |
const { increment, decrement } = useActions(counterReducer, (dispatch) => ({ | |
increment: (amount) => dispatch({ type: 'increment', amount || 1 }), | |
decrement: (amount) => dispatch({ type: 'decrement', amount: -(amount || 1) }) | |
})); | |
const { getCount, getDividedBy } = useSelectors(counterReducer, (state) => ({ | |
getCount: () => state, | |
getDividedBy: (amount) => state.count / amount | |
})); | |
return ( | |
<div> | |
<p>Current count is {getCount()} and divided by two: {getDividedBy(2)}</p> | |
<button onClick={() => increment(1)}>+</button> | |
<button onClick={() => decrement(1)}>-</button> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment