英文:
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])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论