-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChildRouter.js
34 lines (26 loc) · 952 Bytes
/
ChildRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* eslint-disable import/no-dynamic-require */
import React, { useState } from 'react';
// eslint-disable-next-line import/no-unresolved
import { matchPath } from '@reach/router';
import { buildModalElement, buildPageElement } from './utils';
const ChildRouter = props => {
const { pageResources, location, routes } = props;
const [prevProps, setPrevProps] = useState(null);
const [currentProps, setCurrentProps] = useState(null);
const [match] = routes.map(route => matchPath(route, location.pathname)).filter(Boolean);
if (location.pathname !== currentProps?.location?.pathname) {
const oldProps = currentProps;
setCurrentProps(props);
setPrevProps(oldProps);
return null;
}
const pageElement = buildPageElement(match, prevProps, props);
const modalElement = buildModalElement(match, pageResources, props);
return (
<>
{pageElement}
{modalElement}
</>
);
};
export default ChildRouter;