GeezerVison is showing github.com — First commit · DevCollection/fast-react-context@e4b46fd
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Logs | | 2 | +logs | | 3 | +*.log | | 4 | +npm-debug.log* | | 5 | +yarn-debug.log* | | 6 | +yarn-error.log* | | 7 | +pnpm-debug.log* | | 8 | +lerna-debug.log* | | 9 | + | | 10 | +node_modules | | 11 | +dist | | 12 | +dist-ssr | | 13 | +*.local | | 14 | + | | 15 | +# Editor directories and files | | 16 | +.vscode/* | | 17 | +!.vscode/extensions.json | | 18 | +.idea | | 19 | +.DS_Store | | 20 | +*.suo | | 21 | +*.ntvs* | | 22 | +*.njsproj | | 23 | +*.sln | | 24 | +*.sw? |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<!DOCTYPE html> | | 2 | +<html lang="en"> | | 3 | + <head> | | 4 | + <meta charset="UTF-8" /> | | 5 | + <link rel="icon" type="image/svg+xml" href="/vite.svg" /> | | 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | | 7 | + <title>Vite + React + TS</title> | | 8 | + </head> | | 9 | + <body> | | 10 | + <div id="root"></div> | | 11 | + <script type="module" src="/src/main.tsx"></script> | | 12 | + </body> | | 13 | +</html> |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "name": "simple-context", | | 3 | + "private": true, | | 4 | + "version": "0.0.0", | | 5 | + "type": "module", | | 6 | + "scripts": { | | 7 | + "dev": "vite", | | 8 | + "build": "tsc && vite build", | | 9 | + "preview": "vite preview" | | 10 | + }, | | 11 | + "dependencies": { | | 12 | + "react": "^18.2.0", | | 13 | + "react-dom": "^18.2.0" | | 14 | + }, | | 15 | + "devDependencies": { | | 16 | + "@types/react": "^18.0.17", | | 17 | + "@types/react-dom": "^18.0.6", | | 18 | + "@vitejs/plugin-react": "^2.1.0", | | 19 | + "typescript": "^4.6.4", | | 20 | + "vite": "^3.1.0" | | 21 | + } | | 22 | +} |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboardExpand all lines: fast-context-generic/src/App.css Collapse file Copy file name to clipboard +71Lines changed: 71 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import createFastContext from "./createFastContext"; | | 2 | + | | 3 | +const { Provider, useStore } = createFastContext({ | | 4 | + first: "", | | 5 | + last: "", | | 6 | +}); | | 7 | + | | 8 | +const TextInput = ({ value }: { value: "first" | "last" }) => { | | 9 | + const [fieldValue, setStore] = useStore((store) => store[value]); | | 10 | + return ( | | 11 | + <div className="field"> | | 12 | + {value}:{" "} | | 13 | + <input | | 14 | + value={fieldValue} | | 15 | + onChange={(e) => setStore({ [value]: e.target.value })} | | 16 | + /> | | 17 | + </div> | | 18 | + ); | | 19 | +}; | | 20 | + | | 21 | +const Display = ({ value }: { value: "first" | "last" }) => { | | 22 | + const [fieldValue] = useStore((store) => store[value]); | | 23 | + return ( | | 24 | + <div className="value"> | | 25 | + {value}: {fieldValue} | | 26 | + </div> | | 27 | + ); | | 28 | +}; | | 29 | + | | 30 | +const FormContainer = () => { | | 31 | + return ( | | 32 | + <div className="container"> | | 33 | + <h5>FormContainer</h5> | | 34 | + <TextInput value="first" /> | | 35 | + <TextInput value="last" /> | | 36 | + </div> | | 37 | + ); | | 38 | +}; | | 39 | + | | 40 | +const DisplayContainer = () => { | | 41 | + return ( | | 42 | + <div className="container"> | | 43 | + <h5>DisplayContainer</h5> | | 44 | + <Display value="first" /> | | 45 | + <Display value="last" /> | | 46 | + </div> | | 47 | + ); | | 48 | +}; | | 49 | + | | 50 | +const ContentContainer = () => { | | 51 | + return ( | | 52 | + <div className="container"> | | 53 | + <h5>ContentContainer</h5> | | 54 | + <FormContainer /> | | 55 | + <DisplayContainer /> | | 56 | + </div> | | 57 | + ); | | 58 | +}; | | 59 | + | | 60 | +function App() { | | 61 | + return ( | | 62 | + <Provider> | | 63 | + <div className="container"> | | 64 | + <h5>App</h5> | | 65 | + <ContentContainer /> | | 66 | + </div> | | 67 | + </Provider> | | 68 | + ); | | 69 | +} | | 70 | + | | 71 | +export default App; |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
Collapse file Copy file name to clipboard +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import React, { | | 2 | + useRef, | | 3 | + createContext, | | 4 | + useContext, | | 5 | + useCallback, | | 6 | + useSyncExternalStore, | | 7 | +} from "react"; | | 8 | + | | 9 | +export default function createFastContext<Store>(initialState: Store) { | | 10 | + function useStoreData(): { | | 11 | + get: () => Store; | | 12 | + set: (value: Partial<Store>) => void; | | 13 | + subscribe: (callback: () => void) => () => void; | | 14 | + } { | | 15 | + const store = useRef(initialState); | | 16 | + | | 17 | + const get = useCallback(() => store.current, []); | | 18 | + | | 19 | + const subscribers = useRef(new Set<() => void>()); | | 20 | + | | 21 | + const set = useCallback((value: Partial<Store>) => { | | 22 | + store.current = { ...store.current, ...value }; | | 23 | + subscribers.current.forEach((callback) => callback()); | | 24 | + }, []); | | 25 | + | | 26 | + const subscribe = useCallback((callback: () => void) => { | | 27 | + subscribers.current.add(callback); | | 28 | + return () => subscribers.current.delete(callback); | | 29 | + }, []); | | 30 | + | | 31 | + return { | | 32 | + get, | | 33 | + set, | | 34 | + subscribe, | | 35 | + }; | | 36 | + } | | 37 | + | | 38 | + type UseStoreDataReturnType = ReturnType<typeof useStoreData>; | | 39 | + | | 40 | + const StoreContext = createContext<UseStoreDataReturnType | null>(null); | | 41 | + | | 42 | + function Provider({ children }: { children: React.ReactNode }) { | | 43 | + return ( | | 44 | + <StoreContext.Provider value={useStoreData()}> | | 45 | + {children} | | 46 | + </StoreContext.Provider> | | 47 | + ); | | 48 | + } | | 49 | + | | 50 | + function useStore<SelectorOutput>( | | 51 | + selector: (store: Store) => SelectorOutput | | 52 | + ): [SelectorOutput, (value: Partial<Store>) => void] { | | 53 | + const store = useContext(StoreContext); | | 54 | + if (!store) { | | 55 | + throw new Error("Store not found"); | | 56 | + } | | 57 | + | | 58 | + const state = useSyncExternalStore(store.subscribe, () => | | 59 | + selector(store.get()) | | 60 | + ); | | 61 | + | | 62 | + return [state, store.set]; | | 63 | + } | | 64 | + | | 65 | + return { | | 66 | + Provider, | | 67 | + useStore, | | 68 | + }; | | 69 | +} |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +body { | | 2 | + font-family: Arial, Helvetica, sans-serif; | | 3 | + padding: 1rem; | | 4 | +} | | 5 | + | | 6 | +.container { | | 7 | + margin-top: 0.5rem; | | 8 | + padding: 0.5rem 1.5rem; | | 9 | + border: 1px solid #ccc; | | 10 | +} | | 11 | + | | 12 | +.field, .value { | | 13 | + padding: 0.5rem; | | 14 | +} |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import React from 'react' | | 2 | +import ReactDOM from 'react-dom/client' | | 3 | +import App from './App' | | 4 | +import './index.css' | | 5 | + | | 6 | +ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( | | 7 | + <React.StrictMode> | | 8 | + <App /> | | 9 | + </React.StrictMode> | | 10 | +) |
|