@umijs/renderer-react 3.5.20 → 4.0.0-beta.12

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 CHANGED
@@ -1 +1,3 @@
1
1
  # @umijs/renderer-react
2
+
3
+ See our website [umijs](https://umijs.org) for more information.
package/dist/app.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import { Location } from 'history';
3
+ import { Navigator } from 'react-router-dom';
4
+ import { IRoutesById } from './types';
5
+ export declare function App(props: {
6
+ navigator: Navigator;
7
+ location: Location;
8
+ routes: IRoutesById;
9
+ routeComponents: Record<string, any>;
10
+ pluginManager: any;
11
+ }): JSX.Element;
12
+ export declare function RouteComponent(props: {
13
+ id: string;
14
+ }): JSX.Element;
package/dist/app.js ADDED
@@ -0,0 +1,50 @@
1
+ // @ts-ignore
2
+ import loadable from '@loadable/component';
3
+ import React from 'react';
4
+ import { Router, useRoutes } from 'react-router-dom';
5
+ import { AppContext, useAppContext } from './appContext';
6
+ import { createClientRoutes } from './routes';
7
+ export function App(props) {
8
+ const clientRoutes = React.useMemo(() => {
9
+ return createClientRoutes({
10
+ routesById: props.routes,
11
+ Component: RouteComponent,
12
+ });
13
+ }, [props.routes]);
14
+ let ret = (React.createElement(Router, { navigator: props.navigator, location: props.location },
15
+ React.createElement(Routes, null)));
16
+ for (const key of [
17
+ 'innerProvider',
18
+ 'i18nProvider',
19
+ 'dataflowProvider',
20
+ 'outerProvider',
21
+ ]) {
22
+ ret = props.pluginManager.applyPlugins({
23
+ type: 'modify',
24
+ key: key,
25
+ initialValue: ret,
26
+ });
27
+ }
28
+ return (React.createElement(AppContext.Provider, { value: {
29
+ routes: props.routes,
30
+ routeComponents: props.routeComponents,
31
+ pluginManager: props.pluginManager,
32
+ navigator: props.navigator,
33
+ clientRoutes,
34
+ } }, ret));
35
+ }
36
+ function Routes() {
37
+ const { clientRoutes } = useAppContext();
38
+ return useRoutes(clientRoutes) || clientRoutes[0].element;
39
+ }
40
+ function Loading() {
41
+ return React.createElement("div", null, "Loading...");
42
+ }
43
+ export function RouteComponent(props) {
44
+ const loader = useAppContext().routeComponents[props.id];
45
+ const RouteComponent = loadable(loader, {
46
+ fallback: React.createElement(Loading, null),
47
+ });
48
+ // ref: https://reactjs.org/docs/code-splitting.html
49
+ return React.createElement(RouteComponent, null);
50
+ }
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ export interface IAppContextType {
3
+ routes: any;
4
+ routeComponents: any;
5
+ clientRoutes: any;
6
+ pluginManager: any;
7
+ navigator: any;
8
+ }
9
+ export declare const AppContext: React.Context<IAppContextType | undefined>;
10
+ export declare function useAppContext(): IAppContextType;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ export const AppContext = React.createContext(undefined);
3
+ export function useAppContext() {
4
+ return React.useContext(AppContext);
5
+ }
@@ -0,0 +1,12 @@
1
+ import { IRoutesById } from './types';
2
+ export declare function Browser(props: {
3
+ routes: IRoutesById;
4
+ routeComponents: Record<string, any>;
5
+ pluginManager: any;
6
+ }): any;
7
+ export declare function renderClient(opts: {
8
+ rootElement?: HTMLElement;
9
+ routes: IRoutesById;
10
+ routeComponents: Record<string, any>;
11
+ pluginManager: any;
12
+ }): void;
@@ -0,0 +1,27 @@
1
+ import { createBrowserHistory } from 'history';
2
+ import React from 'react';
3
+ import ReactDOM from 'react-dom';
4
+ import { App } from './app';
5
+ export function Browser(props) {
6
+ const historyRef = React.useRef();
7
+ if (historyRef.current === undefined) {
8
+ historyRef.current = createBrowserHistory({ window });
9
+ }
10
+ const history = historyRef.current;
11
+ const [state, dispatch] = React.useReducer((_, update) => update, {
12
+ action: history.action,
13
+ location: history.location,
14
+ });
15
+ React.useLayoutEffect(() => history.listen(dispatch), [history]);
16
+ return props.pluginManager.applyPlugins({
17
+ type: 'modify',
18
+ key: 'rootContainer',
19
+ initialValue: (React.createElement(App, { navigator: history, location: state.location, routes: props.routes, routeComponents: props.routeComponents, pluginManager: props.pluginManager })),
20
+ args: {},
21
+ });
22
+ }
23
+ export function renderClient(opts) {
24
+ // @ts-ignore
25
+ const root = ReactDOM.createRoot(opts.rootElement || document.getElementById('root'));
26
+ root.render(React.createElement(Browser, { routes: opts.routes, routeComponents: opts.routeComponents, pluginManager: opts.pluginManager }));
27
+ }
package/dist/index.d.ts CHANGED
@@ -1,35 +1,3 @@
1
- import { History, Location } from 'history-with-query';
2
- import { FunctionComponent } from 'react';
3
- import { match } from 'react-router-dom';
4
- export interface IComponent extends FunctionComponent {
5
- getInitialProps?: Function;
6
- preload?: () => Promise<any>;
7
- }
8
- export interface IRoute {
9
- path?: string;
10
- exact?: boolean;
11
- redirect?: string;
12
- component?: IComponent | string;
13
- routes?: IRoute[];
14
- key?: any;
15
- strict?: boolean;
16
- sensitive?: boolean;
17
- wrappers?: any[];
18
- [k: string]: any;
19
- }
20
- export interface IRouteComponentProps<Params extends {
21
- [K in keyof Params]?: string;
22
- } = {}, Query extends {
23
- [K in keyof Query]?: string;
24
- } = {}> {
25
- children: JSX.Element;
26
- location: Location & {
27
- query: Query;
28
- };
29
- route: IRoute;
30
- routes: IRoute[];
31
- history: History;
32
- match: match<Params>;
33
- }
34
- export { default as renderClient } from './renderClient/renderClient';
35
- export { default as renderRoutes } from './renderRoutes/renderRoutes';
1
+ export { Link, Outlet, useNavigate } from 'react-router-dom';
2
+ export { useAppContext } from './appContext';
3
+ export { renderClient } from './browser';