简体   繁体   English

根据 url 参数设置状态 react-router v6

[英]Set state based on url params react-router v6

okay so I have this form where the user can query a search with type "radio buttons" search input and a react-select filled with categories, the button works as a link front end form for clarification好的,所以我有这个表单,用户可以在其中查询类型为“单选按钮”搜索输入和一个反应选择填充类别的搜索,该按钮用作链接前端表单以进行澄清

{selectedOption ?
<Link to={`/annonser${typeToRoute}${selectedOptionToRoute}${searchToRoute}`} >
                    <button>Hitta annons / mönster</button>
                    </Link>: <Link to={`/annonser${typeToRoute}${searchToRoute}`} >
                    <button>Hitta annons / mönster</button>

where I then want to query db to get data based on the url params with useParams hook and then set the state然后我想查询 db 以使用 useParams 钩子根据 url 参数获取数据,然后设置状态

const { type, category, search} = useParams();
    const categoryRef = db.collection("articles").where("subCategory", "==", category)

I want the user to be able to search any of the alternatives seperatly aswell I will not use any form validation to make sure the user have everything not null.我希望用户能够单独搜索任何替代方案,我不会使用任何表单验证来确保用户的所有内容都不为空。 Is this the right way to go about it?这是正确的方法吗? Because you cant have optional params in route component in v6 for some reason?因为某种原因,您在 v6 的路由组件中不能有可选参数? Is there any other way to achieve what im trying to do or am I on the right track?有没有其他方法可以实现我想要做的事情,或者我是否在正确的轨道上? Thanks in advance never done anything like this before.提前感谢从未做过这样的事情。 Let me know if my question is asked poorly since I dont have alot of experience asking questions here.如果我的问题问得不好,请告诉我,因为我在这里没有很多提问的经验。

If you want optional route path parameters then see this answer , the gist being you declare a route for each that can match.如果您想要可选的路由路径参数,请查看此答案,要点是您为每个可以匹配的路由声明一个路由。 The issue here though is that the order matters, "type" would need to be provided for "category" to be optional, and both "type" and "category" would need to be provided for "search" to be optional.但是这里的问题是顺序很重要,需要提供“类型”才能使“类别”成为可选,并且需要同时提供“类型”和“类别”才能使“搜索”成为可选。

Here's an example showing why this isn't ideal:这是一个示例,说明为什么这并不理想:

<Route path="/annonser/:type" element={.....} />
<Route path="/annonser/:type/:category" element={.....} />
<Route path="/annonser/:type/:category/:search" element={.....} />

This is ok until you want the combination of "type" and "search".在您想要“类型”和“搜索”的组合之前,这是可以的。 If you try path="/annonser/:type/:search" then this has the same specificity as path="/annonser/:type/:category" , so whichever route is listed first is the one that'll be matched.如果您尝试使用path="/annonser/:type/:search" ,那么这与path="/annonser/:type/:category"具有相同的特异性,因此首先列出的路线就是要匹配的路线。

Using the queryString method then all or none, and anything between, can be provided/optional and valid.使用 queryString 方法,可以提供/可选且有效。

Example:例子:

  • Route <Route path="/annonser" element={.....} />路线<Route path="/annonser" element={.....} />
  • URL "annonser?type=customType&category=foo&search=bar" URL "annonser?type=customType&category=foo&search=bar"

Use the useSearchParams hook to access queryString parameters.使用useSearchParams挂钩访问 queryString 参数。

const [searchParams] = useSearchParams();

...

const type = searchParams.get("type");         // "customType"
const category = searchParams.get("category"); // "foo"
const search = searchParams.get("search");     // "bar"

const categoryRef = db.collection("articles").where("subCategory", "==", category)

Any queryString parameter not provided, when queried, will return null .任何未提供的 queryString 参数在查询时都将返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM