基于URL参数React-Router V6设置状态

发布于 2025-02-11 18:58:07 字数 893 浏览 1 评论 0原文

好的,所以我有此表格,用户可以在其中查询使用类型“无线电按钮”输入的搜索和一个填充类别的反应选择,该按钮可作为链接澄清的前端表单

{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>

然后我想查询db以使用useparams钩子根据URL参数获取

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

数据能够单独搜索任何替代方案,因为我不会使用任何表单验证来确保用户的所有内容都不为空。这是正确的方法吗?因为由于某种原因,您无法在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

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? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2025-02-18 18:58:07

如果您想要可选的路由路径参数,请查看此答案,要点是您声明每个可以匹配的路由。不过,这里的问题是,要为“类别”提供“类型”,“类型”是可选的,并且需要为“搜索”提供“类型”和“类别”。

这是一个示例,说明为什么这不是理想的:

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

这是可以的,直到您想要“类型”和“搜索”的组合。如果您尝试path =“/annonser/:type/:search”,则具有与path =“/annonser/:type/:category”的特异性,所以首先列出的任何路线都是与之匹配的路线。

使用Querystring方法,然后全部或无,并且可以提供任何内容,可以提供/可选和有效。

示例:

  • 路由&lt;路由路径=“/annonser” element = {.....}/&gt;
  • url “ annonser?type = casturetype&amp&amp; category = foo&amp; search = bar''

使用 austp.eptryparams 访问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)

查询时未提供任何查询参数,将返回null

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.

Using the queryString method then all or none, and anything between, can be provided/optional and valid.

Example:

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

Use the useSearchParams hook to access queryString parameters.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文