urlParamsForPath function inlined in Router

This commit is contained in:
NikolaBorislavovHristov 2019-09-13 17:05:36 +03:00
parent d94b8cd4f7
commit 698f1174c7
2 changed files with 10 additions and 14 deletions

View file

@ -4,7 +4,6 @@ const PropTypes = require('prop-types');
const UrlUtils = require('url');
const Route = require('../Route');
const { RoutesContainerProvider } = require('../RoutesContainerContext');
const urlParamsForPath = require('./urlParamsForPath');
const Router = ({ className, onPathNotMatch, ...props }) => {
const [{ homePath, viewsConfig }] = React.useState(() => ({
@ -61,7 +60,16 @@ const Router = ({ className, onPathNotMatch, ...props }) => {
return;
}
const urlParams = urlParamsForPath(routeConfig, pathname);
const matches = pathname.match(routeConfig.regexp);
const urlParams = routeConfig.urlParamsNames.reduce((urlParams, name, index) => {
if (Array.isArray(matches) && typeof matches[index + 1] === 'string') {
urlParams[name] = matches[index + 1];
} else {
urlParams[name] = null;
}
return urlParams;
}, {});
const routeViewIndex = viewsConfig.findIndex((vc) => vc.includes(routeConfig));
const routeIndex = viewsConfig[routeViewIndex].findIndex((rc) => rc === routeConfig);
setViews((views) => {

View file

@ -1,12 +0,0 @@
module.exports = (routeConfig, path) => {
const matches = typeof path === 'string' ? path.match(routeConfig.regexp) : [];
return routeConfig.urlParamsNames.reduce((urlParams, name, index) => {
if (Array.isArray(matches) && typeof matches[index + 1] === 'string') {
urlParams[name] = matches[index + 1];
} else {
urlParams[name] = null;
}
return urlParams;
}, {});
};