Download react-use/stories/createReducerContext.story.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/stories/createReducerContext.story.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-use/stories/createReducerContext.story.tsx
-
curl -L -o createReducerContext.story.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/stories/createReducerContext.story.tsx
1.56 kB
| 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 ( | |
| <p> | |
| Component A | |
| <button type="button" onClick={() => dispatch('decrement')}> | |
| - | |
| </button> | |
| {count} | |
| <button type="button" onClick={() => dispatch('increment')}> | |
| + | |
| </button> | |
| </p> | |
| ); | |
| }; | |
| const ComponentB = () => { | |
| const [count, dispatch] = useSharedCounter(); | |
| return ( | |
| <p> | |
| Component B | |
| <button type="button" onClick={() => dispatch('decrement')}> | |
| - | |
| </button> | |
| {count} | |
| <button type="button" onClick={() => dispatch('increment')}> | |
| + | |
| </button> | |
| </p> | |
| ); | |
| }; | |
| const Demo = () => { | |
| return ( | |
| <SharedCounterProvider> | |
| <p>Those two counters share the same value.</p> | |
| <ComponentA /> | |
| <ComponentB /> | |
| </SharedCounterProvider> | |
| ); | |
| }; | |
| storiesOf('State/createReducerContext', module) | |
| .add('Docs', () => <ShowDocs md={require('../docs/createReducerContext.md')} />) | |
| .add('Demo', () => <Demo />); | |