# `createReducerContext` Factory for react context hooks that will behave just like [React's `useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer) except the state will be shared among all components in the provider. This allows you to have a shared state that any component can update easily. ## Usage An example with two counters that shared the same value. ```jsx import { createReducerContext } from 'react-use'; type Action = 'increment' | 'decrement'; const reducer = (state: number, action: Action) => { switch (action) { case 'increment': return state + 1; case 'decrement': return state - 1; default: throw new Error(); } }; const [useSharedCounter, SharedCounterProvider] = createReducerContext(reducer, 0); const ComponentA = () => { const [count, dispatch] = useSharedCounter(); return (

Component A    {count} 

); }; const ComponentB = () => { const [count, dispatch] = useSharedCounter(); return (

Component B    {count} 

); }; const Demo = () => { return (

Those two counters share the same value.

); }; ``` ## Reference ```jsx const [useSharedState, SharedStateProvider] = createReducerContext(reducer, initialState); // In wrapper const Wrapper = ({ children }) => ( // You can override the initial state for each Provider { children } ) // In a component const Component = () => { const [sharedState, dispatch] = useSharedState(); // ... } ```