@real-router/preact 0.0.1
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/README.md +194 -0
- package/dist/cjs/index.d.ts +84 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/metafile-cjs.json +1 -0
- package/dist/esm/index.d.mts +84 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/metafile-esm.json +1 -0
- package/package.json +76 -0
- package/src/RouterProvider.tsx +59 -0
- package/src/components/Link.tsx +102 -0
- package/src/components/RouteView/RouteView.tsx +39 -0
- package/src/components/RouteView/components.tsx +13 -0
- package/src/components/RouteView/helpers.tsx +88 -0
- package/src/components/RouteView/index.ts +7 -0
- package/src/components/RouteView/types.ts +17 -0
- package/src/constants.ts +9 -0
- package/src/context.ts +10 -0
- package/src/hooks/useIsActiveRoute.tsx +34 -0
- package/src/hooks/useNavigator.tsx +15 -0
- package/src/hooks/useRoute.tsx +15 -0
- package/src/hooks/useRouteNode.tsx +30 -0
- package/src/hooks/useRouteUtils.tsx +12 -0
- package/src/hooks/useRouter.tsx +15 -0
- package/src/hooks/useRouterTransition.tsx +19 -0
- package/src/hooks/useStableValue.tsx +8 -0
- package/src/index.ts +35 -0
- package/src/types.ts +33 -0
- package/src/useSyncExternalStore.ts +32 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useEffect, useState } from "preact/hooks";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Polyfill for React's useSyncExternalStore.
|
|
5
|
+
*
|
|
6
|
+
* Preact does not provide a native useSyncExternalStore.
|
|
7
|
+
* This implementation uses useState + useEffect to subscribe
|
|
8
|
+
* to external stores.
|
|
9
|
+
*
|
|
10
|
+
* Race condition handling: the value may change between
|
|
11
|
+
* `useState(getSnapshot)` (render) and `useEffect` (commit).
|
|
12
|
+
* We synchronize by calling `setValue(getSnapshot())` before
|
|
13
|
+
* subscribing in the effect.
|
|
14
|
+
*/
|
|
15
|
+
export function useSyncExternalStore<T>(
|
|
16
|
+
subscribe: (onStoreChange: () => void) => () => void,
|
|
17
|
+
getSnapshot: () => T,
|
|
18
|
+
_getServerSnapshot?: () => T,
|
|
19
|
+
): T {
|
|
20
|
+
const [value, setValue] = useState(getSnapshot);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
// Synchronize before subscribing to handle race condition
|
|
24
|
+
setValue(getSnapshot());
|
|
25
|
+
|
|
26
|
+
return subscribe(() => {
|
|
27
|
+
setValue(getSnapshot());
|
|
28
|
+
});
|
|
29
|
+
}, [subscribe, getSnapshot]);
|
|
30
|
+
|
|
31
|
+
return value;
|
|
32
|
+
}
|