@shuvi/router-react 1.0.0-rc.5 → 1.0.0-rc.8

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/esm/Router.js CHANGED
@@ -1,11 +1,10 @@
1
1
  import * as React from 'react';
2
- import { useRef, useReducer } from 'react';
3
2
  import * as PropTypes from 'prop-types';
4
3
  import invariant from '@shuvi/utils/lib/invariant';
4
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
5
5
  import { RouterContext, RouteContext } from './contexts';
6
6
  import { useInRouterContext } from './hooks';
7
7
  import { __DEV__ } from './constants';
8
- import { useIsomorphicEffect } from './utils';
9
8
  /**
10
9
  * Provides location context for the rest of the app.
11
10
  *
@@ -22,19 +21,13 @@ export function Router({ children = null, static: staticProp = false, router })
22
21
  router: router
23
22
  };
24
23
  }, [staticProp, router]);
25
- const unmount = useRef(false);
26
- const forceupdate = useReducer(s => s * -1, 1)[1];
27
- useIsomorphicEffect(() => () => (unmount.current = true), []);
28
- useIsomorphicEffect(() => {
29
- router.listen(() => {
30
- if (unmount.current) {
31
- return;
32
- }
33
- forceupdate();
34
- });
35
- }, [router]);
24
+ const { subscribe, getSnapshot } = React.useMemo(() => ({
25
+ subscribe: (fn) => router.listen(fn),
26
+ getSnapshot: () => router.current
27
+ }), [router]);
28
+ const current = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
36
29
  return (React.createElement(RouterContext.Provider, { value: contextVal },
37
- React.createElement(RouteContext.Provider, { children: children, value: router.current })));
30
+ React.createElement(RouteContext.Provider, { children: children, value: current })));
38
31
  }
39
32
  if (__DEV__) {
40
33
  Router.displayName = 'Router';
package/lib/Router.js CHANGED
@@ -2,13 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Router = void 0;
4
4
  const React = require("react");
5
- const react_1 = require("react");
6
5
  const PropTypes = require("prop-types");
7
6
  const invariant_1 = require("@shuvi/utils/lib/invariant");
7
+ const shim_1 = require("use-sync-external-store/shim");
8
8
  const contexts_1 = require("./contexts");
9
9
  const hooks_1 = require("./hooks");
10
10
  const constants_1 = require("./constants");
11
- const utils_1 = require("./utils");
12
11
  /**
13
12
  * Provides location context for the rest of the app.
14
13
  *
@@ -25,19 +24,13 @@ function Router({ children = null, static: staticProp = false, router }) {
25
24
  router: router
26
25
  };
27
26
  }, [staticProp, router]);
28
- const unmount = (0, react_1.useRef)(false);
29
- const forceupdate = (0, react_1.useReducer)(s => s * -1, 1)[1];
30
- (0, utils_1.useIsomorphicEffect)(() => () => (unmount.current = true), []);
31
- (0, utils_1.useIsomorphicEffect)(() => {
32
- router.listen(() => {
33
- if (unmount.current) {
34
- return;
35
- }
36
- forceupdate();
37
- });
38
- }, [router]);
27
+ const { subscribe, getSnapshot } = React.useMemo(() => ({
28
+ subscribe: (fn) => router.listen(fn),
29
+ getSnapshot: () => router.current
30
+ }), [router]);
31
+ const current = (0, shim_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
39
32
  return (React.createElement(contexts_1.RouterContext.Provider, { value: contextVal },
40
- React.createElement(contexts_1.RouteContext.Provider, { children: children, value: router.current })));
33
+ React.createElement(contexts_1.RouteContext.Provider, { children: children, value: current })));
41
34
  }
42
35
  exports.Router = Router;
43
36
  if (constants_1.__DEV__) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/router-react",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -28,15 +28,17 @@
28
28
  "node": ">= 12.0.0"
29
29
  },
30
30
  "dependencies": {
31
- "@shuvi/router": "1.0.0-rc.5",
32
- "@shuvi/platform-shared": "1.0.0-rc.5",
33
- "@shuvi/utils": "1.0.0-rc.5",
34
- "prop-types": "^15.8.1"
31
+ "@shuvi/router": "1.0.0-rc.8",
32
+ "@shuvi/platform-shared": "1.0.0-rc.8",
33
+ "@shuvi/utils": "1.0.0-rc.8",
34
+ "prop-types": "^15.8.1",
35
+ "use-sync-external-store": "1.1.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@types/prop-types": "^15.7.5",
38
39
  "@types/react": "18.0.9",
39
40
  "@types/react-test-renderer": "18.0.0",
41
+ "@types/use-sync-external-store": "0.0.3",
40
42
  "react-test-renderer": "^18.1.0",
41
43
  "react": "18.1.0",
42
44
  "react-dom": "18.1.0"