import { storiesOf } from '@storybook/react'; import * as React from 'react'; import { createReducerContext } from '../src'; import ShowDocs from './util/ShowDocs'; 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.