|
1 | 1 | import React, { useEffect } from 'react';
|
2 |
| -import { ApplyPluginsType, Plugin, NativeRouter } from 'umi-react-native-runtime'; |
3 |
| -// import { matchRoutes } from 'react-router-config'; |
4 |
| -import { IRoute } from '../IRoute'; |
| 2 | +import { AppRegistry } from 'react-native'; |
| 3 | +import { ApplyPluginsType, Plugin, Router } from 'umi-react-native-runtime'; |
| 4 | +import { matchRoutes } from 'react-router-config'; |
| 5 | +import { IRoute } from '..'; |
5 | 6 | import renderRoutes from '../renderRoutes/renderRoutes';
|
6 | 7 |
|
7 | 8 | interface IRouterComponentProps {
|
8 | 9 | routes: IRoute[];
|
9 | 10 | plugin: Plugin;
|
| 11 | + history: any; |
10 | 12 | }
|
11 | 13 |
|
12 | 14 | interface IOpts extends IRouterComponentProps {
|
13 |
| - rootElement?: string | HTMLElement; |
| 15 | + appKey: string; |
14 | 16 | }
|
15 | 17 |
|
16 | 18 | function RouterComponent(props: IRouterComponentProps) {
|
| 19 | + const { history, ...renderRoutesProps } = props; |
| 20 | + |
17 | 21 | useEffect(() => {
|
18 |
| - // function routeChangeHandler(location: any, action?: string) { |
19 |
| - // const matchedRoutes = matchRoutes(props.routes, location.pathname); |
20 |
| - // |
21 |
| - // props.plugin.applyPlugins({ |
22 |
| - // key: 'onRouteChange', |
23 |
| - // type: ApplyPluginsType.event, |
24 |
| - // args: { |
25 |
| - // routes: props.routes, |
26 |
| - // matchedRoutes, |
27 |
| - // location, |
28 |
| - // action, |
29 |
| - // }, |
30 |
| - // }); |
31 |
| - // } |
32 |
| - // routeChangeHandler(history.location, 'POP'); |
33 |
| - // return history.listen(routeChangeHandler); |
| 22 | + function routeChangeHandler(location: any, action?: string) { |
| 23 | + const matchedRoutes = matchRoutes(props.routes, location.pathname); |
| 24 | + |
| 25 | + props.plugin.applyPlugins({ |
| 26 | + key: 'onRouteChange', |
| 27 | + type: ApplyPluginsType.event, |
| 28 | + args: { |
| 29 | + routes: props.routes, |
| 30 | + matchedRoutes, |
| 31 | + location, |
| 32 | + action, |
| 33 | + }, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + routeChangeHandler(history.location, 'POP'); |
| 38 | + return history.listen(routeChangeHandler); |
34 | 39 | }, [history]);
|
35 | 40 |
|
36 |
| - return <NativeRouter>{renderRoutes(props)}</NativeRouter>; |
| 41 | + return <Router history={history}>{renderRoutes(renderRoutesProps)}</Router>; |
37 | 42 | }
|
38 | 43 |
|
39 | 44 | export default function renderClient(opts: IOpts) {
|
40 | 45 | const rootContainer = opts.plugin.applyPlugins({
|
41 | 46 | type: ApplyPluginsType.modify,
|
42 | 47 | key: 'rootContainer',
|
43 |
| - initialValue: <RouterComponent routes={opts.routes} plugin={opts.plugin} />, |
| 48 | + initialValue: <RouterComponent history={opts.history} routes={opts.routes} plugin={opts.plugin} />, |
44 | 49 | args: {
|
| 50 | + history: opts.history, |
45 | 51 | routes: opts.routes,
|
46 | 52 | plugin: opts.plugin,
|
47 | 53 | },
|
48 | 54 | });
|
49 |
| - |
| 55 | + AppRegistry.registerComponent(opts.appKey, rootContainer); |
50 | 56 | return rootContainer;
|
51 | 57 | }
|
0 commit comments