如何在相同路径上替换URL中的查询? | 如果可能的话,不使用useHistory()

huangapple go评论52阅读模式
英文:

How do I replace queries in the URL when on the same path? | Without useHistory() if possible

问题

以下是您要翻译的内容:

我有一个页面,根据URL的搜索参数显示从我的API返回的产品。例如,'/products?search=socks'将返回标题中包含socks的产品。搜索栏位于页眉中。如果我在主页上搜索产品(不同路径:'/'),它会导航到产品列表页面并显示产品。但是,如果我在产品列表页面上搜索产品(相同路径:'/products'),它会重新渲染并删除搜索参数,导致不显示任何产品。我更喜欢使用当前的钩子,因为似乎useHistory已过时。

我尝试直接导航到URL:

navigate(`/products?search=${search}`);

我还尝试设置参数,如一份建议:

const params = new URLSearchParams();
params.set('search', search);
navigate(`/products?search=${search}`);

我期望query/search参数替换搜索值并重新渲染以显示新的结果。

英文:

I have a page that displays products returned from my API based on the URL's search parameters. For example, '/products?search=socks' would return products with the word socks in the title. The search bar is located in the header. If I search for a product on the home page (a different path: '/'), it navigates to the product list page and displays the products. However, if I search for a product on the product list page (the same path: '/products'), it re-renders and removes the search parameters, leading to no products being displayed. I prefer to use current hooks as it appears useHistory is outdated.

I have tried to navigate to the URL directly:

navigate(`/products?search=${search}`);

And I have tried to set the parameters as one source suggested:

const params = new URLSearchParams();
params.set('search', search);
navigate(`/products?search=${search}`);

I expected the query/search parameter to replace the search value and re-render with the new results.

答案1

得分: 2

I would use the new React Router V6 useSearchParams hook:

import { useSearchParams } from 'react-router-dom'

Then instantiate the hook at the top of your component:

let [searchParams, setSearchParams] = useSearchParams();

Then you can manipulate the search params anywhere, in your case:

setSearchParams({ search: '...');

Doing this alongside the navigate() call will basically lead the user to the page as the search param is being changed.

In your situation, I would detect if the user is already on the product list page before always running navigate(). You can use the useLocation() hook to detect what pathname you currently are on.

If you're on the product list page already, don't run navigate(). Your useEffect would be responsible for re-rendering the page when search params change using a dependency array [searchParams.search]:

useEffect(() => {
  // do stuff when search params "search" variable changes
}, [searchParams.search])
英文:

I would use the new React Router V6 useSearchParams hook:

import { useSearchParams } from 'react-router-dom'

Then instantiate the hook at the top of your component:

let [searchParams, setSearchParams] = useSearchParams();

Then you can manipulate the search params anywhere, in your case:

setSearchParams({ search: '...' );

Doing this along-side the navigate() call will basicallly lead the user to the page as the search param is being changed.

In your situation, I would detect if the user is already on the product list page before always running navigate(). You can use the useLocation() hook to detect what pathname you currently are on.

If you're on the product list page already, don't run navigate(). Your useEffect would be responsible for re-rendering the page when search params change using a dependency array [searchParams.search]:

useEffect(() => {
  // do stuff when search params "search" variable changes
}, [searchParams.search])

huangapple
  • 本文由 发表于 2023年2月16日 02:44:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定