routes distinct by path instead of timestamp

This commit is contained in:
NikolaBorislavovHristov 2018-06-29 17:42:41 +03:00
parent dbeb0dde87
commit 93bf64fa7b

View file

@ -6,10 +6,9 @@ class Router extends Component {
constructor(props) {
super(props);
this.historyLength = history.length;
this.state = {
views: Array.apply(null, { length: props.config.views.length }).map(() => ({
id: -1,
path: null,
element: null
}))
};
@ -28,12 +27,10 @@ class Router extends Component {
return nextState.views !== this.state.views;
}
onLocationChanged = ({ timeStamp: id } = { timeStamp: 0 }) => {
onLocationChanged = () => {
const hashIndex = window.location.href.indexOf('#');
const hashPath = hashIndex === -1 ? '' : window.location.href.substring(hashIndex + 1);
const path = joinPaths('/', hashPath);
const isPushAction = this.historyLength < history.length;
this.historyLength = history.length;
this.props.config.views.forEach((viewConfig, viewConfigIndex) => {
viewConfig.routes.forEach((routeConfig) => {
if (matchPath(path, routeConfig)) {
@ -41,18 +38,14 @@ class Router extends Component {
views: views.map((view, viewIndex) => {
if (viewIndex > viewConfigIndex) {
return {
id: -1,
path: null,
element: null
};
} else if (viewIndex === viewConfigIndex) {
if (!isPushAction && React.isValidElement(view.element) && view.element.type === routeConfig.component) {
return view;
} else {
return {
id,
element: React.createElement(routeConfig.component)
};
}
return {
path,
element: React.createElement(routeConfig.component)
};
} else {
return view;
}
@ -69,7 +62,7 @@ class Router extends Component {
{
this.state.views
.filter(({ element }) => React.isValidElement(element))
.map(({ id, element }) => <div key={id} className={this.props.routeContainerClassName}>{element}</div>)
.map(({ path, element }) => <div key={path} className={this.props.routeContainerClassName}>{element}</div>)
}
</div>
);