我似乎找不到如何更新查询参数与反应路由器不使用<Link/>。hashHistory.push(url)似乎没有注册查询参数,而且似乎不能将查询对象或任何东西作为第二个参数传递。
如何将url从/shop/Clothes/dresses更改为/shop/Clothes/dresses?color=blue在反应路由器没有使用<链接>?
onChange函数真的是侦听查询更改的唯一方法吗?为什么不自动检测和响应查询更改-以参数更改的方式?
我似乎找不到如何更新查询参数与反应路由器不使用<Link/>。hashHistory.push(url)似乎没有注册查询参数,而且似乎不能将查询对象或任何东西作为第二个参数传递。
如何将url从/shop/Clothes/dresses更改为/shop/Clothes/dresses?color=blue在反应路由器没有使用<链接>?
onChange函数真的是侦听查询更改的唯一方法吗?为什么不自动检测和响应查询更改-以参数更改的方式?
当前回答
我做了一个简单的钩子来减轻工作。
让我们想象一下你的url是这样的: /搜索吗?起源=主页= 1
function useUrl(param: string) {
const history = useHistory()
const { search, pathname } = useLocation()
const url = new URLSearchParams(search)
const urlParam = url.get(param)
const [value, setValue] = useState(urlParam !== null ? urlParam : '')
function _setValue(val: string){
url.set(param, val)
history.replace({ pathname, search: url.toString() });
setValue(val)
}
return [value, _setValue]
}
那么实际使用情况:
function SearchPage() {
const [origin] = useUrl("origin")
const [page, setPage] = useUrl("page")
return (
<div>
<p>Return to: {origin}</p>
<p>Current Page: {page}</p>
</div>
)
}
其他回答
我目前在一个正在运行的项目中使用react-router v5,不容易迁移到v6。 我写了一个钩子,允许读取和修改一个URL参数,同时保持其他URL参数不变。 数组被视为逗号分隔值的列表: magnifying_glass ?产品=管,猎鹿帽
import { useCallback } from 'react';
import { useHistory } from 'react-router';
const getDecodedUrlParam = (name: string, locationSearch: string, _default?: any) => {
const params = deserialize(locationSearch);
const param = params[name];
if (_default && Array.isArray(_default)) {
return param
? param.split(',').map((v: string) => decodeURIComponent(v))
: _default;
}
return param ? decodeURIComponent(param) : _default;
};
const deserialize = (locationSearch: string): any => {
if (locationSearch.startsWith('?')) {
locationSearch = locationSearch.substring(1);
}
const parts = locationSearch.split('&');
return Object.fromEntries(parts.map((part) => part.split('=')));
};
const serialize = (params: any) =>
Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join('&');
export const useURLSearchParam = (name: string, _default?: any) => {
const history = useHistory();
const value: any = getDecodedUrlParam(name, location.search, _default);
const _update = useCallback(
(value: any) => {
const params = deserialize(location.search);
if (Array.isArray(value)) {
params[name] = value.map((v) => encodeURIComponent(v)).join(',');
} else {
params[name] = encodeURIComponent(value);
}
history.replace({ pathname: location.pathname, search: serialize(params) });
},
[history, name]
);
const _delete = useCallback(() => {
const params = deserialize(location.search);
delete params[name];
history.replace({ pathname: location.pathname, search: serialize(params) });
}, [history, name]);
return [value, _update, _delete];
};
在hashHistory的push方法中,可以指定查询参数。例如,
history.push({
pathname: '/dresses',
search: '?color=blue'
})
or
history.push('/dresses?color=blue')
您可以查看这个存储库以获得有关使用历史记录的其他示例
就像@Craques解释的那样,我们可以使用替换功能,而不是每次更改都推送一个新路由。然而,在react-router的第6版中,useHistory()被useNavigate()取代,它只返回一个函数。你可以将选项传递给函数,以达到与旧的location.replace()相同的效果:
import { useLocation, useNavigate } from 'react-router-dom';
const to = { pathname: location.pathname, search: newParams.toString() };
navigate(to, { replace: true });
我做了一个简单的钩子来减轻工作。
让我们想象一下你的url是这样的: /搜索吗?起源=主页= 1
function useUrl(param: string) {
const history = useHistory()
const { search, pathname } = useLocation()
const url = new URLSearchParams(search)
const urlParam = url.get(param)
const [value, setValue] = useState(urlParam !== null ? urlParam : '')
function _setValue(val: string){
url.set(param, val)
history.replace({ pathname, search: url.toString() });
setValue(val)
}
return [value, _setValue]
}
那么实际使用情况:
function SearchPage() {
const [origin] = useUrl("origin")
const [page, setPage] = useUrl("page")
return (
<div>
<p>Return to: {origin}</p>
<p>Current Page: {page}</p>
</div>
)
}
使用React Router V6,我们可以像这样实现它
import { useNavigate, createSearchParams } from 'react-router-dom';
/* In React Component */
const navigate = useNavigate();
const params = {
color: 'blue',
};
const options = {
pathname: '/shop/Clothes/dresses',
search: `?${createSearchParams(params)}`,
};
navigate(options, { replace: true });