@manyducks.co/hookshot 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +138 -0
- package/dist/main.d.ts +33 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +71 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 That's a lot of ducks, LLC
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# @manyducks.co/hookshot
|
|
2
|
+
|
|
3
|
+
Hookshot is a simple state management library for React that makes it easy to share state between components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @manyducks.co/hookshot
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Example: Counter
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createStore } from "@manyducks.co/hookshot";
|
|
15
|
+
import { useState, useCallback } from "react";
|
|
16
|
+
|
|
17
|
+
type CounterOptions = {
|
|
18
|
+
initialValue?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Define a store; get a provider and a dedicated hook.
|
|
22
|
+
|
|
23
|
+
const [CounterProvider, useCounter] = createStore((options: CounterOptions) => {
|
|
24
|
+
const [value, setValue] = useState(options.initialValue ?? 0);
|
|
25
|
+
|
|
26
|
+
const increment = useCallback((amount = 1) => {
|
|
27
|
+
setValue((current) => current + amount);
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
const decrement = useCallback((amount = 1) => {
|
|
31
|
+
setValue((current) => current - amount);
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
const reset = useCallback(() => {
|
|
35
|
+
setValue(0);
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
value,
|
|
40
|
+
increment,
|
|
41
|
+
decrement,
|
|
42
|
+
reset,
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
function MyApp() {
|
|
47
|
+
return (
|
|
48
|
+
// One instance of your store is created wherever you render the provider.
|
|
49
|
+
// Multiple `<CounterProvider>`s in different parts of your app will each maintain their own state.
|
|
50
|
+
<CounterProvider options={{ initialValue: 51 }}>
|
|
51
|
+
<CounterDisplay />
|
|
52
|
+
<CounterControls />
|
|
53
|
+
</CounterProvider>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function CounterDisplay() {
|
|
58
|
+
// All children can access the shared state with the dedicated hook.
|
|
59
|
+
// TypeScript will automatically infer the correct return types here.
|
|
60
|
+
const { value } = useCounter();
|
|
61
|
+
|
|
62
|
+
return <p>Count is: {value}</p>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function CounterControls() {
|
|
66
|
+
// Same instance of the counter store.
|
|
67
|
+
// These functions will alter the value that CounterDisplay sees.
|
|
68
|
+
const { increment, decrement, reset } = useCounter();
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div>
|
|
72
|
+
<button onClick={increment}>+1</button>
|
|
73
|
+
<button onClick={decrement}>-1</button>
|
|
74
|
+
<button onClick={reset}>Reset</button>
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Optimizing with a selector
|
|
81
|
+
|
|
82
|
+
It's typical to only need part of the state. A component that calls `useCounter` will render every time the store renders, even if it's not using the part of the state that changed. You can pass a selector function to pluck only what you care about so your component will render just when you need it to.
|
|
83
|
+
|
|
84
|
+
Let's optimize the components.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
function CounterDisplay() {
|
|
88
|
+
const value = useCounter((state) => state.value);
|
|
89
|
+
// We select only the value to display.
|
|
90
|
+
// If we add more state to the counter store later, this component won't even notice.
|
|
91
|
+
|
|
92
|
+
return <p>Count is: {value}</p>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function CounterControls() {
|
|
96
|
+
const [increment, decrement, reset] = useCounter((state) => [
|
|
97
|
+
state.increment,
|
|
98
|
+
state.decrement,
|
|
99
|
+
state.reset,
|
|
100
|
+
]);
|
|
101
|
+
// We don't care about the value here, only the functions to modify it.
|
|
102
|
+
// Because we've wrapped them in `useCallback` their references will remain stable.
|
|
103
|
+
// Changes to the counter value will never cause this component to render.
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<div>
|
|
107
|
+
<button onClick={increment}>+1</button>
|
|
108
|
+
<button onClick={decrement}>-1</button>
|
|
109
|
+
<button onClick={reset}>Reset</button>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Memoize everything
|
|
116
|
+
|
|
117
|
+
> [!IMPORTANT]
|
|
118
|
+
> Because Hookshot relies on referential equality when comparing selected state, you must memoize any selected functions and derived objects returned by your hook.
|
|
119
|
+
|
|
120
|
+
If you return a new function or object reference on every render, components selecting those values will also re-render every time, defeating the selector optimization.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// ❌ Bad: This creates a new function reference every render!
|
|
124
|
+
const increment = (amount = 1) => setValue((current) => current + amount);
|
|
125
|
+
|
|
126
|
+
// ✅ Good: The reference remains stable.
|
|
127
|
+
const increment = useCallback((amount = 1) => {
|
|
128
|
+
setValue((current) => current + amount);
|
|
129
|
+
}, []);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Prior art
|
|
133
|
+
|
|
134
|
+
We have been long time users of the great [unstated-next](https://github.com/jamiebuilds/unstated-next). Hookshot was created to add memoization and an improved API on top of that same idea.
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
This code is provided under the MIT license.
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Infers the correct Provider props based on the hook's arguments.
|
|
3
|
+
* - No arguments -> { children }
|
|
4
|
+
* - Required argument -> { options: Options, children }
|
|
5
|
+
* - Optional argument -> { options?: Options, children }
|
|
6
|
+
*/
|
|
7
|
+
export type ProviderProps<F extends (...args: any) => any> = Parameters<F> extends [] ? {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
} : [
|
|
10
|
+
] extends Parameters<F> ? {
|
|
11
|
+
options?: Parameters<F>[0];
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
} : {
|
|
14
|
+
options: Parameters<F>[0];
|
|
15
|
+
children: React.ReactNode;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Provides a single instance to all its children.
|
|
19
|
+
*/
|
|
20
|
+
export type SkyhookProvider<F extends (...args: any) => any> = React.ComponentType<ProviderProps<F>>;
|
|
21
|
+
/**
|
|
22
|
+
* Plucks only the parts of the state this component cares about.
|
|
23
|
+
*/
|
|
24
|
+
export type Selector<Value, Selected> = (value: Value) => Selected;
|
|
25
|
+
/**
|
|
26
|
+
* Accesses the nearest parent instance.
|
|
27
|
+
*/
|
|
28
|
+
export interface Skyhook<Value> {
|
|
29
|
+
(): Value;
|
|
30
|
+
<Selected>(select: Selector<Value, Selected>): Selected;
|
|
31
|
+
}
|
|
32
|
+
export declare function createHook<F extends (...args: any) => any>(fn: F): [SkyhookProvider<F>, Skyhook<ReturnType<F>>];
|
|
33
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,IAEvD,UAAU,CAAC,CAAC,CAAC,SAAS,EAAE,GACpB;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,GAE7B;CAAE,SAAS,UAAU,CAAC,CAAC,CAAC,GACtB;IAAE,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAAC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,GAEzD;IAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAAC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,CAAC;AAEjE;;GAEG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,IACzD,KAAK,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AAExC;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,KAAK,EAAE,QAAQ,IAAI,CAAC,KAAK,EAAE,KAAK,KAAK,QAAQ,CAAC;AAEnE;;GAEG;AACH,MAAM,WAAW,OAAO,CAAC,KAAK;IAC5B,IAAI,KAAK,CAAC;IACV,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC;CACzD;AA0BD,wBAAgB,UAAU,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,EACxD,EAAE,EAAE,CAAC,GACJ,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CA8D9C"}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createContext as y, useRef as a, useLayoutEffect as d, createElement as p, useContext as v, useSyncExternalStore as g } from "react";
|
|
2
|
+
const h = /* @__PURE__ */ Symbol();
|
|
3
|
+
class j {
|
|
4
|
+
value;
|
|
5
|
+
listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
constructor(e) {
|
|
7
|
+
this.value = e;
|
|
8
|
+
}
|
|
9
|
+
update(e) {
|
|
10
|
+
this.value = e;
|
|
11
|
+
}
|
|
12
|
+
notify() {
|
|
13
|
+
this.listeners.forEach((e) => e());
|
|
14
|
+
}
|
|
15
|
+
subscribe = (e) => (this.listeners.add(e), () => this.listeners.delete(e));
|
|
16
|
+
get = () => this.value;
|
|
17
|
+
}
|
|
18
|
+
function k(r) {
|
|
19
|
+
const e = y(h);
|
|
20
|
+
function c(t) {
|
|
21
|
+
const n = r(t.options), s = a(null);
|
|
22
|
+
return s.current ? s.current.update(n) : s.current = new j(n), d(() => {
|
|
23
|
+
s.current.notify();
|
|
24
|
+
}, [n]), p(e.Provider, {
|
|
25
|
+
value: s.current,
|
|
26
|
+
children: t.children
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function l(t) {
|
|
30
|
+
const n = v(e);
|
|
31
|
+
if (n === h)
|
|
32
|
+
throw new Error("Component must be wrapped with a skyhook <Provider>");
|
|
33
|
+
const s = a(
|
|
34
|
+
null
|
|
35
|
+
), f = () => {
|
|
36
|
+
const u = n.get();
|
|
37
|
+
if (!t) return u;
|
|
38
|
+
const o = s.current;
|
|
39
|
+
if (o && Object.is(o.root, u))
|
|
40
|
+
return o.selected;
|
|
41
|
+
const i = t(u);
|
|
42
|
+
return o && O(i, o.selected) ? (s.current = { root: u, selected: o.selected }, o.selected) : (s.current = { root: u, selected: i }, i);
|
|
43
|
+
};
|
|
44
|
+
return g(n.subscribe, f, f);
|
|
45
|
+
}
|
|
46
|
+
return [c, l];
|
|
47
|
+
}
|
|
48
|
+
function O(r, e) {
|
|
49
|
+
if (Object.is(r, e)) return !0;
|
|
50
|
+
if (typeof r != "object" || r === null || typeof e != "object" || e === null)
|
|
51
|
+
return !1;
|
|
52
|
+
if (Array.isArray(r)) {
|
|
53
|
+
if (!Array.isArray(e) || r.length !== e.length) return !1;
|
|
54
|
+
for (let t = 0; t < r.length; t++)
|
|
55
|
+
if (!Object.is(r[t], e[t])) return !1;
|
|
56
|
+
return !0;
|
|
57
|
+
}
|
|
58
|
+
if (r.constructor !== e.constructor) return !1;
|
|
59
|
+
const c = Object.keys(r);
|
|
60
|
+
if (c.length !== Object.keys(e).length) return !1;
|
|
61
|
+
const l = Object.prototype.hasOwnProperty;
|
|
62
|
+
for (let t = 0; t < c.length; t++) {
|
|
63
|
+
const n = c[t];
|
|
64
|
+
if (!l.call(e, n) || !Object.is(r[n], e[n]))
|
|
65
|
+
return !1;
|
|
66
|
+
}
|
|
67
|
+
return !0;
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
k as createHook
|
|
71
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@manyducks.co/hookshot",
|
|
3
|
+
"description": "Memoized context stores for React",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"author": "tony@manyducks.co",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/manyducksco/hookshot.git"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/main.js",
|
|
13
|
+
"types": "dist/main.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/main.d.ts",
|
|
17
|
+
"import": "./dist/main.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "vite build && tsc",
|
|
22
|
+
"test": "vitest",
|
|
23
|
+
"lint": "eslint . --max-warnings 0",
|
|
24
|
+
"check-types": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@testing-library/react": "^16.3.2",
|
|
28
|
+
"@types/node": "^22.19.11",
|
|
29
|
+
"@types/react": "19.2.2",
|
|
30
|
+
"@types/react-dom": "19.2.2",
|
|
31
|
+
"eslint": "^9.39.1",
|
|
32
|
+
"jsdom": "^28.1.0",
|
|
33
|
+
"typescript": "5.9.2",
|
|
34
|
+
"vite": "^7.3.1",
|
|
35
|
+
"vitest": "^4.0.18"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"react": ">=18",
|
|
39
|
+
"react-dom": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
]
|
|
44
|
+
}
|