|
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom'; |
| 3 | + |
| 4 | +import createFastContext from '../src/index'; |
| 5 | + |
| 6 | +import './index.css'; |
| 7 | + |
| 8 | + |
| 9 | +const { Provider, useStore } = createFastContext({ |
| 10 | + first: '', |
| 11 | + last: '', |
| 12 | +}); |
| 13 | + |
| 14 | +const { Provider: ProviderTitle, useStore: useStoreTitle } = createFastContext({ |
| 15 | + title: 'App Fast Context', |
| 16 | +}); |
| 17 | + |
| 18 | +const TextInput = ({ value }: { value: 'first' | 'last' }) => { |
| 19 | + const [fieldValue, setStore] = useStore((store) => store[value]); |
| 20 | + return ( |
| 21 | + <div className='field'> |
| 22 | + {value}:{' '} |
| 23 | + <input |
| 24 | + value={fieldValue} |
| 25 | + onChange={(e) => setStore({ [value]: e.target.value })} |
| 26 | + /> |
| 27 | + </div> |
| 28 | + ); |
| 29 | +}; |
| 30 | + |
| 31 | +const Display = ({ value }: { value: 'first' | 'last' }) => { |
| 32 | + const [fieldValue] = useStore((store) => store[value]); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className='value'> |
| 36 | + {value}: {fieldValue} |
| 37 | + </div> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +const FormContainer = () => { |
| 42 | + return ( |
| 43 | + <div className='container'> |
| 44 | + <h5>FormContainer</h5> |
| 45 | + <TextInput value='first' /> |
| 46 | + <TextInput value='last' /> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const DisplayContainer = () => { |
| 52 | + return ( |
| 53 | + <div className='container'> |
| 54 | + <h5>DisplayContainer</h5> |
| 55 | + <Display value='first' /> |
| 56 | + <Display value='last' /> |
| 57 | + </div> |
| 58 | + ); |
| 59 | +}; |
| 60 | + |
| 61 | +const ContentContainer = () => { |
| 62 | + return ( |
| 63 | + <div className='container'> |
| 64 | + <h5>ContentContainer</h5> |
| 65 | + <FormContainer /> |
| 66 | + <DisplayContainer /> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +const Title = () => { |
| 72 | + const [title] = useStoreTitle((store) => store.title); |
| 73 | + |
| 74 | + return <h5>{title}</h5>; |
| 75 | +}; |
| 76 | + |
| 77 | +function App() { |
| 78 | + return ( |
| 79 | + <Provider> |
| 80 | + <div className='container'> |
| 81 | + <ProviderTitle> |
| 82 | + <Title /> |
| 83 | + </ProviderTitle> |
| 84 | + <ContentContainer /> |
| 85 | + </div> |
| 86 | + </Provider> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +ReactDOM.render(<App />, document.getElementById('root')); |
0 commit comments