gpt4 book ai didi

javascript - 如何在 react-router v4 中保留查询参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:25:52 28 4
gpt4 key购买 nike

用户在登录后重定向到我的应用程序(java 上的服务器),他们有 url,看起来像这样 http://10.8.0.29:8083/html/?locale=RU&token=1c5c71f2-dcda-4a51-8cf6-f8f6ff1031d0&returnTo=http://10.8.0.23:8080/

(带有一些参数,html - 是源所在的文件夹)。我需要在我的应用程序上导航时保留此参数。到目前为止,除了这个 How to redirect with react-router while preserving initial query parameters? 之外,我还没有找到解决这个问题的任何简单方法。老问题,所以我希望再次提出这个问题。提前致谢。

最佳答案

我已经分享了my solution对于您的问题的评论中的 react-router v3。

以下是我对 react-router v4 的解决方案。

使用以下 createPreserveQueryHistory 函数覆盖 history.pushhistory.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
}
}

关于javascript - 如何在 react-router v4 中保留查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43613140/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com