|
1 | | -# react-exo-hooks |
| 1 | +# React exo Hooks |
2 | 2 |
|
3 | | -To install dependencies: |
| 3 | +## useUnsaved |
| 4 | +Prevent user navigation/window closing when there are unsaved changes (NextJS router compatible) |
| 5 | +```ts |
| 6 | +function Component () { |
| 7 | + const [isUnsaved, setIsUnsaved] = useState(false) |
4 | 8 |
|
5 | | -```bash |
6 | | -bun install |
| 9 | + useUnsaved(isUnsaved) |
| 10 | +} |
7 | 11 | ``` |
8 | 12 |
|
9 | | -To run: |
| 13 | +## useDebouncedState |
| 14 | +Debounce state changes with a delay |
| 15 | +```tsx |
| 16 | +function Component () { |
| 17 | + const [debounced, setState, real] = useDebouncedState('', 300) |
10 | 18 |
|
11 | | -```bash |
12 | | -bun run index.ts |
| 19 | + useEffect(() => { |
| 20 | + console.log(`Debounced value: ${debounced}`) |
| 21 | + }, [debounced]) |
| 22 | + |
| 23 | + return ( |
| 24 | + <input value={real} onChange={(e) => setState(e.currentTarget.value)} /> |
| 25 | + ) |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## usePromise |
| 30 | +Handle a promise within render behavior |
| 31 | +```ts |
| 32 | +function Component () { |
| 33 | + const { |
| 34 | + // 'waiting' | 'resolved' | 'rejected' |
| 35 | + state, |
| 36 | + result, |
| 37 | + error |
| 38 | + } = usePromise(() => signedIn && ((abortSignal) -> getUserProfile(abortSignal))) |
| 39 | +} |
13 | 40 | ``` |
14 | 41 |
|
15 | | -This project was created using `bun init` in bun v1.2.17. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. |
| 42 | +## useSet |
| 43 | +A Set. Rerenders upon mutation. Listen for update signal with `+` |
| 44 | +> [!TIP] |
| 45 | +> Force state update with `set.forceUpdate()` |
| 46 | +```tsx |
| 47 | +function Component () { |
| 48 | + const set = useSet<string>() |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + console.log('set items:') |
| 52 | + for (const item of set) console.log(item) |
| 53 | + }, [set, +set]) |
| 54 | + |
| 55 | + return ( |
| 56 | + <button onClick={() => set.add('foo')}>CLICK ME</button> |
| 57 | + ) |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## useMap |
| 62 | +A Map. Rerenders upon mutation. Listen for update signal with `+` |
| 63 | +> [!TIP] |
| 64 | +> Force state update with `map.forceUpdate()` |
| 65 | +```tsx |
| 66 | +function Component () { |
| 67 | + const map = useMap<string, number>() |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + console.log('map items:') |
| 71 | + for (const [key, value] of map.entries()) console.log(`[${key}]: ${value}`) |
| 72 | + }, [map, +map]) |
| 73 | + |
| 74 | + return ( |
| 75 | + <button onClick={() => map.set('foo', 52)}>CLICK ME</button> |
| 76 | + ) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## useArray |
| 81 | +An Array. Rerenders upon mutation. Listen for update signal with `+` |
| 82 | +> [!TIP] |
| 83 | +> Force state update with `array.forceUpdate()` |
| 84 | +```tsx |
| 85 | +function Component () { |
| 86 | + const array = useArray<string>() |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + console.log('array items:', array.join(', ')) |
| 90 | + }, [array, +array]) |
| 91 | + |
| 92 | + return ( |
| 93 | + <button onClick={() => array.push('foo')}>CLICK ME</button> |
| 94 | + ) |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## useObject |
| 99 | +An Object. Rerenders on mutation. Will recursively listen on simple children (not class instances). |
| 100 | +> [!TIP] |
| 101 | +> Force state update with `forceUpdate()`, the third item of the tuple |
| 102 | +```tsx |
| 103 | +function Component () { |
| 104 | + const [object, setObject] = useObject({ foo: { bar: 'baz' } }) |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + console.log('Object updated!', object) |
| 108 | + }, [object, +object]) |
| 109 | + |
| 110 | + return ( |
| 111 | + <button onClick={() => { object.foo.bar = 'foobar' }}>CLICK ME</button> |
| 112 | + ) |
| 113 | +} |
| 114 | +``` |
0 commit comments