Skip to content

Commit 3bd8ed1

Browse files
committed
Add use-object
1 parent bc4582b commit 3bd8ed1

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

hooks/use-object.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useMemo, useState } from 'react'
2+
3+
/**
4+
* Create an object state value that auto updates on mutation
5+
* @param initial The initial object
6+
* @returns [object, setObject]
7+
*/
8+
export function useObject<T extends object> (initial: T): [object: T, setObject: React.Dispatch<React.SetStateAction<T>>] {
9+
const [signal, setSignal] = useState(0)
10+
const [object, setObject] = useState(initial)
11+
12+
const proxy = useMemo(() => new Proxy(object, {
13+
set (target, prop, newValue) {
14+
if (prop !== 'valueOf' && target[prop as keyof typeof target] !== newValue) setSignal((prior) => prior + 1)
15+
target[prop as keyof typeof target] = newValue
16+
return true
17+
},
18+
19+
deleteProperty (target, prop) {
20+
if (prop in target) setSignal((prior) => prior + 1)
21+
delete target[prop as keyof typeof target]
22+
return true
23+
}
24+
}), [object])
25+
26+
proxy.valueOf = () => signal
27+
return [proxy, setObject]
28+
}

hooks/use-promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type PromiseResult<T, P extends boolean, E = unknown> = {
2222
* @param persist Persist result values and error values into states that wouldn't normally have them
2323
* @returns An object containing the state and settled values
2424
*/
25-
export function useFetch<T, P extends boolean, E = unknown> (
25+
export function usePromise<T, P extends boolean, E = unknown> (
2626
fn: () => false | undefined | null | '' | ((signal?: AbortSignal) => Promise<T>),
2727
deps: React.DependencyList = [],
2828
persist?: P

index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export {
88
useSet
99
} from './hooks/use-set'
1010

11+
export {
12+
type PromiseResult,
13+
usePromise
14+
} from './hooks/use-promise'
15+
16+
export {
17+
useObject
18+
} from './hooks/use-object'
19+
1120
export {
1221
useDebouncedState
1322
} from './hooks/use-debounced-state'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "react-exo-hooks",
33
"module": "index.ts",
4+
"main": "dist/index.js",
45
"devDependencies": {
56
"@types/bun": "latest",
67
"@types/next": "^9.0.0",

tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
// Bundler mode
1212
"moduleResolution": "bundler",
13-
"allowImportingTsExtensions": true,
13+
// "allowImportingTsExtensions": true,
1414
"verbatimModuleSyntax": true,
15-
"noEmit": true,
15+
// "noEmit": true,
16+
"declaration": true,
17+
"outDir": "dist/",
1618

1719
// Best practices
1820
"strict": true,

0 commit comments

Comments
 (0)