我已经在对您的问题的评论中分享了我的 react-router v3解决方案。
以下是我对 react-router v4 的解决方案。
使用以下createPreserveQueryHistory
函数来覆盖history.push
和history.replace
方法,以便它们保留指定的查询参数:
import queryString from 'query-string';
import {createBrowserHistory} from 'history'
function preserveQueryParameters(history, preserve, location) {
const currentQuery = queryString.parse(history.location.search);
if (currentQuery) {
const preservedQuery = {};
for (let p of preserve) {
const v = currentQuery[p];
if (v) {
preservedQuery[p] = v;
}
}
if (location.search) {
Object.assign(preservedQuery, queryString.parse(location.search));
}
location.search = queryString.stringify(preservedQuery);
}
return location;
}
function createLocationDescriptorObject(location, state) {
return typeof location === 'string' ? { pathname: location, state } : location;
}
function createPreserveQueryHistory(createHistory, queryParameters) {
return (options) => {
const history = createHistory(options);
const oldPush = history.push, oldReplace = history.replace;
history.push = (path, state) => oldPush.apply(history, [preserveQueryParameters(history, queryParameters, createLocationDescriptorObject(path, state))]);
history.replace = (path, state) => oldReplace.apply(history, [preserveQueryParameters(history, queryParameters, createLocationDescriptorObject(path, state))]);
return history;
};
}
const history = createPreserveQueryHistory(createBrowserHistory, ['locale', 'token', 'returnTo'])();
然后在路由器定义中使用它:
<Router history={history}>
...
</Router>
对于那些使用 TypeScript 的人:
import {History, LocationDescriptor, LocationDescriptorObject} from 'history'
import queryString from 'query-string'
import LocationState = History.LocationState
type CreateHistory<O, H> = (options?: O) => History & H
function preserveQueryParameters(history: History, preserve: string[], location: LocationDescriptorObject): LocationDescriptorObject {
const currentQuery = queryString.parse(history.location.search)
if (currentQuery) {
const preservedQuery: { [key: string]: unknown } = {}
for (let p of preserve) {
const v = currentQuery[p]
if (v) {
preservedQuery[p] = v
}
}
if (location.search) {
Object.assign(preservedQuery, queryString.parse(location.search))
}
location.search = queryString.stringify(preservedQuery)
}
return location
}
function createLocationDescriptorObject(location: LocationDescriptor, state?: LocationState): LocationDescriptorObject {
return typeof location === 'string' ? {pathname: location, state} : location
}
export function createPreserveQueryHistory<O, H>(createHistory: CreateHistory<O, H>,
queryParameters: string[]): CreateHistory<O, H> {
return (options?: O) => {
const history = createHistory(options)
const oldPush = history.push, oldReplace = history.replace
history.push = (path: LocationDescriptor, state?: LocationState) =>
oldPush.apply(history, [preserveQueryParameters(history, queryParameters, createLocationDescriptorObject(path, state))])
history.replace = (path: LocationDescriptor, state?: LocationState) =>
oldReplace.apply(history, [preserveQueryParameters(history, queryParameters, createLocationDescriptorObject(path, state))])
return history
}
}