Skip to content

Commit 87d8eb8

Browse files
Add useSharedStateSelector hook
Introduced a new `useSharedStateSelector` hook to enhance state management capabilities by enabling efficient state selection with memoization. Added `react-fast-compare` to improve deep comparison performance. - Added `useSharedStateSelector` for selecting and memoizing shared state slices. - Used `react-fast-compare` for optimized equality checks. - Updated `package.json` and `pnpm-lock.yaml` to include `react-fast-compare`. - Removed unused `pnpm-workspace.yaml`. - Updated `vite` version to `7.1.5` to align with dependencies.
1 parent ab61d2c commit 87d8eb8

File tree

6 files changed

+143
-275
lines changed

6 files changed

+143
-275
lines changed

demo/app.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
SharedStatesProvider,
55
sharedSubscriptionsApi,
66
useSharedFunction,
7-
useSharedState,
7+
useSharedState, useSharedStateSelector,
88
useSharedSubscription
99
} from 'react-shared-states';
1010
import './FakeSharedEmitter';
@@ -79,6 +79,44 @@ const Comp2 = () => {
7979
)
8080
}
8181

82+
const Comp3 = () => {
83+
const [s, set] = useSharedState("json", {a: 0, b: 0});
84+
85+
86+
return (
87+
<div>
88+
<button onClick={() => set(a => ({...a, a: a.a+1}))}>plus a</button>
89+
<br/>
90+
<button onClick={() => set(a => ({...a, b: a.b+1}))}>plus b</button>
91+
<br/>
92+
results: {JSON.stringify(s)}
93+
<br/>
94+
</div>
95+
)
96+
}
97+
98+
const Comp4 = () => {
99+
const a = useSharedStateSelector<{a: number, b: number}, "json", number>("json", (j) => j.a);
100+
console.log("render a");
101+
return (
102+
<div>
103+
a: {a}
104+
<br/>
105+
</div>
106+
)
107+
}
108+
109+
const Comp5 = () => {
110+
const a = useSharedStateSelector<{a: number, b: number}, "json", number>("json", (j) => j.b);
111+
console.log("render b");
112+
return (
113+
<div>
114+
b: {a}
115+
<br/>
116+
</div>
117+
)
118+
}
119+
82120
const App = () => {
83121

84122
const [hide, setHide] = useState(false);
@@ -112,6 +150,12 @@ const App = () => {
112150
<Comp1/>
113151
<Comp1/>
114152
</SharedStatesProvider>
153+
<br/>
154+
<br/>
155+
<br/>
156+
<Comp3/>
157+
<Comp4/>
158+
<Comp5/>
115159
</div>
116160
);
117161
};

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
"bugs": {
3434
"url": "https://github.com/HichemTab-tech/react-shared-states/issues"
3535
},
36+
"peerDependencies": {
37+
"react": "^19.1.11",
38+
"react-dom": "^19.1.11"
39+
},
3640
"homepage": "https://github.com/HichemTab-tech/react-shared-states#readme",
3741
"devDependencies": {
3842
"@testing-library/dom": "^10.4.1",
@@ -45,9 +49,12 @@
4549
"react": "^19.1.1",
4650
"react-dom": "^19.1.1",
4751
"typescript": "^5.9.2",
48-
"vite": "^7.1.3",
52+
"vite": "7.1.5",
4953
"vite-plugin-banner": "^0.8.1",
5054
"vite-plugin-dts": "^4.5.4",
5155
"vitest": "^3.2.4"
56+
},
57+
"dependencies": {
58+
"react-fast-compare": "^3.2.2"
5259
}
5360
}

0 commit comments

Comments
 (0)