Fix hashbang behaviour (#1095)

This commit is contained in:
Mike Grabowski
2017-04-18 15:25:12 +02:00
committed by GitHub
parent 2bbfc03cd0
commit ba98f7a1ae
2 changed files with 16 additions and 4 deletions

View File

@@ -31,14 +31,21 @@ module.exports = (NavigationAwareView) => {
};
}
componentWillUpdate(props, state) {
const {path} = NavigationAwareView.router.getPathAndParamsForState(state);
const uri = `/${path}`;
const {path, params} = NavigationAwareView.router.getPathAndParamsForState(state);
const maybeHash = params && params.hash ? `#${params.hash}` : '';
const uri = `/${path}${maybeHash}`;
if (window.location.pathname !== uri) {
window.history.pushState({}, state.title, uri);
}
const navigation = addNavigationHelpers({state: state.routes[state.index], dispatch: this.dispatch});
document.title = NavigationAwareView.router.getScreenOptions(navigation).title;
}
componentDidUpdate() {
const {params} = NavigationAwareView.router.getPathAndParamsForState(this.state);
if (params && params.hash) {
document.getElementById(params.hash).scrollIntoView();
}
}
dispatch = (action) => {
const state = NavigationAwareView.router.getStateForAction(action, this.state);

View File

@@ -24,8 +24,13 @@ const Linkable = (Inner) => {
} else if (href) {
const match = href.match(/^\/(.*)/);
if (match) {
const path = match[1];
const action = this.context.getActionForPathAndParams(path);
const pathParts = match[1].split('#');
const path = pathParts[0];
let params = {};
if (pathParts.length) {
params.hash = pathParts[1];
}
const action = this.context.getActionForPathAndParams(path, params);
return action;
}
return null;