Skip to content

Commit

Permalink
fix(react): Do a better job matching up route to sync, fixes #20363 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
elylucas committed Feb 14, 2020
1 parent b6fbe98 commit c0aadd6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Expand Up @@ -4,7 +4,7 @@ import { ViewStacks } from './ViewStacks';

export interface RouteManagerContextState {
syncView: (page: HTMLElement, viewId: string) => void;
syncRoute: (id: string, route: any) => void;
syncRoute: (route: any) => void;
hideView: (viewId: string) => void;
viewStacks: ViewStacks;
setupIonRouter: (id: string, children: ReactNode, routerOutlet: HTMLIonRouterOutletElement) => void;
Expand Down
8 changes: 6 additions & 2 deletions packages/react-router/src/ReactRouter/Router.tsx
Expand Up @@ -368,13 +368,17 @@ export class RouteManager extends React.Component<RouteManagerProps, RouteManage
}
}

syncRoute(_id: string, routerOutlet: any) {
syncRoute(routerOutlet: any) {
const ionRouterOutlet = React.Children.only(routerOutlet) as React.ReactElement;

React.Children.forEach(ionRouterOutlet.props.children, (child: React.ReactElement) => {
for (const routeKey in this.routes) {
const route = this.routes[routeKey];
if (typeof route.props.path !== 'undefined' && route.props.path === (child.props.path || child.props.from)) {
if (
((route.props.path || route.props.from) === (child.props.path || child.props.from)) &&
(route.props.exact === child.props.exact) &&
(route.props.to === child.props.to)
) {
this.routes[routeKey] = child;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/src/ReactRouter/StackManager.tsx
Expand Up @@ -32,7 +32,7 @@ class StackManagerInner extends React.Component<StackManagerProps, StackManagerS
}

static getDerivedStateFromProps(props: StackManagerProps, state: StackManagerState) {
props.routeManager.syncRoute('', props.children);
props.routeManager.syncRoute(props.children);
return state;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/react-router/src/ReactRouter/View.tsx
@@ -1,5 +1,6 @@
import { IonLifeCycleContext, NavContext } from '@ionic/react';
import React from 'react';
import { Redirect } from 'react-router';

import { isDevMode } from '../utils';

Expand All @@ -19,6 +20,21 @@ export class View extends React.Component<ViewProps, {}> {
context!: React.ContextType<typeof IonLifeCycleContext>;
ionPage?: HTMLElement;

componentDidMount() {
/**
* If we can tell if view is a redirect, hide it so it will work again in future
*/
const { view, route } = this.props;
if (route.type === Redirect) {
this.props.onHideView(view.id);
} else if (route.props.render && !view.isIonRoute) {
// Test the render to see if it returns a redirect
if (route.props.render().type === Redirect) {
this.props.onHideView(view.id);
}
}
}

componentWillUnmount() {
if (this.ionPage) {
this.ionPage.removeEventListener('ionViewWillEnter', this.ionViewWillEnterHandler.bind(this));
Expand Down

0 comments on commit c0aadd6

Please sign in to comment.