mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
Fix hashbang behaviour (#1095)
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user