|
| 1 | +import { useRef } from "react"; |
| 2 | +import { getStore, ICompareStore, ICompareStoreActions } from "./store"; |
| 3 | +import { ErrorBoundary } from "react-error-boundary"; |
| 4 | +import styles from "./compare.module.scss"; |
| 5 | + |
| 6 | +interface StoreTypeProps { |
| 7 | + type: "zustand" | "kosha"; |
| 8 | +} |
| 9 | + |
| 10 | +type ComponentProps = ICompareStore & ICompareStoreActions; |
| 11 | + |
| 12 | +const P1 = ({ p1, setP1 }: Pick<ComponentProps, "p1" | "setP1">) => { |
| 13 | + const renderCountRef = useRef(0); |
| 14 | + renderCountRef.current++; |
| 15 | + return ( |
| 16 | + <div> |
| 17 | + <p className="display"> |
| 18 | + p1: |
| 19 | + <button onClick={() => setP1(crypto.randomUUID())}>Update</button> |
| 20 | + {p1.slice(25)} | renderCount is {renderCountRef.current} |
| 21 | + </p> |
| 22 | + </div> |
| 23 | + ); |
| 24 | +}; |
| 25 | + |
| 26 | +const P2 = ({ p2, setP2 }: Pick<ComponentProps, "p2" | "setP2">) => { |
| 27 | + const renderCountRef = useRef(0); |
| 28 | + renderCountRef.current++; |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + <p className="display"> |
| 32 | + p2: |
| 33 | + <button onClick={() => setP2(Math.random())}>Update</button> |
| 34 | + {p2.toFixed(6)} | renderCount is {renderCountRef.current} |
| 35 | + </p> |
| 36 | + </div> |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +const P1ExtractAsObj = ({ type }: StoreTypeProps) => { |
| 41 | + const { p1, setP1 } = getStore(type)(({ p1, setP1 }) => ({ p1, setP1 })); |
| 42 | + return <P1 {...{ p1, setP1 }} />; |
| 43 | +}; |
| 44 | + |
| 45 | +const P1ExtractAsArray = ({ type }: StoreTypeProps) => { |
| 46 | + const [p1, setP1] = getStore(type)(({ p1, setP1 }) => [p1, setP1] as const); |
| 47 | + return <P1 {...{ p1, setP1 }} />; |
| 48 | +}; |
| 49 | + |
| 50 | +const P1ExtractIndividually = ({ type }: StoreTypeProps) => { |
| 51 | + const p1 = getStore(type)(({ p1 }) => p1); |
| 52 | + const setP1 = getStore(type)(({ setP1 }) => setP1); |
| 53 | + return <P1 {...{ p1, setP1 }} />; |
| 54 | +}; |
| 55 | + |
| 56 | +const P2ExtractAsObj = ({ type }: StoreTypeProps) => { |
| 57 | + const { p2, setP2 } = getStore(type)(({ p2, setP2 }) => ({ p2, setP2 })); |
| 58 | + return <P2 {...{ p2, setP2 }} />; |
| 59 | +}; |
| 60 | + |
| 61 | +const P2ExtractAsArray = ({ type }: StoreTypeProps) => { |
| 62 | + const [p2, setP2] = getStore(type)(({ p2, setP2 }) => [p2, setP2] as const); |
| 63 | + return <P2 {...{ p2, setP2 }} />; |
| 64 | +}; |
| 65 | + |
| 66 | +const P2ExtractIndividually = ({ type }: StoreTypeProps) => { |
| 67 | + const p2 = getStore(type)(({ p2 }) => p2); |
| 68 | + const setP2 = getStore(type)(({ setP2 }) => setP2); |
| 69 | + return <P2 {...{ p2, setP2 }} />; |
| 70 | +}; |
| 71 | + |
| 72 | +export const Compare = () => ( |
| 73 | + <div className={styles.compare}> |
| 74 | + <div className={styles.compare__store}> |
| 75 | + <h2>Kosha</h2> |
| 76 | + <h4>Extract as Object</h4> |
| 77 | + <code>{`const { p2, setP2 } = myStore(({ p2, setP2 }) => ({ p2, setP2 }))`}</code> |
| 78 | + <P1ExtractAsObj type="kosha" /> |
| 79 | + <P2ExtractAsObj type="kosha" /> |
| 80 | + <hr /> |
| 81 | + <h4>Extract as Array</h4> |
| 82 | + <code>{`const [p2, setP2] = myStore(({ p2, setP2 }) => [p2, setP2] as const)`}</code> |
| 83 | + <P1ExtractAsArray type="kosha" /> |
| 84 | + <P2ExtractAsArray type="kosha" /> |
| 85 | + <hr /> |
| 86 | + <h4>Extract individually</h4> |
| 87 | + <code>{`const p2 = myStore(({ p2 }) => p2)`}</code> |
| 88 | + <P1ExtractIndividually type="kosha" /> |
| 89 | + <P2ExtractIndividually type="kosha" /> |
| 90 | + </div> |
| 91 | + <div className={styles.compare__store}> |
| 92 | + <h2>Zustand</h2> |
| 93 | + <h4>Extract as Object</h4> |
| 94 | + <code>{`const { p2, setP2 } = myStore(({ p2, setP2 }) => ({ p2, setP2 }))`}</code> |
| 95 | + <ErrorBoundary fallback={<p>⚠️Something went wrong</p>}> |
| 96 | + <P1ExtractAsObj type="zustand" /> |
| 97 | + </ErrorBoundary> |
| 98 | + <ErrorBoundary fallback={<p>⚠️Something went wrong</p>}> |
| 99 | + <P2ExtractAsObj type="zustand" /> |
| 100 | + </ErrorBoundary> |
| 101 | + <hr /> |
| 102 | + <h4>Extract as Array</h4> |
| 103 | + <code>{`const [p2, setP2] = myStore(({ p2, setP2 }) => [p2, setP2] as const)`}</code> |
| 104 | + <ErrorBoundary fallback={<p>⚠️Something went wrong</p>}> |
| 105 | + <P1ExtractAsArray type="zustand" /> |
| 106 | + </ErrorBoundary> |
| 107 | + <ErrorBoundary fallback={<p>⚠️Something went wrong</p>}> |
| 108 | + <P2ExtractAsArray type="zustand" /> |
| 109 | + </ErrorBoundary> |
| 110 | + <hr /> |
| 111 | + <h4>Extract individually</h4> |
| 112 | + <code>{`const p2 = myStore(({ p2 }) => p2)`}</code> |
| 113 | + <P1ExtractIndividually type="zustand" /> |
| 114 | + <P2ExtractIndividually type="zustand" /> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | +); |
0 commit comments