简体   繁体   English

React 路由器 v6:更新链接中的 url 参数

[英]React router v6 : update url param in Link

i'm learning react and i'm facing something I don't understand about the Link component.我正在学习 React,我正面临一些我不了解 Link 组件的事情。

I'm making a simple survey with two buttons that links to previous and next question.我正在使用两个链接到上一个和下一个问题的按钮进行简单调查。 Here is the architecture of my app:这是我的应用程序的架构:

ReactDOM.render(
   <React.StrictMode>
      <BrowserRouter>
         <Header />
         <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/survey/:questionNumber" element={<Survey />} />
            <Route path="/results" element={<Results />} />
            <Route path="*" element={<Error />} />
         </Routes>
      </BrowserRouter>
   </React.StrictMode>,
   document.getElementById("root")
);

My component Survey look like this:我的组件 Survey 如下所示:

import { useParams, Link } from "react-router-dom";

function Survey() {
   const { questionNumber } = useParams();
   let questionNumberInt = Number(questionNumber);

   const prevQuestionNumber =
      questionNumberInt === 1 ? null : questionNumberInt - 1;
   const nextQuestionNumber = questionNumberInt + 1;

   return (
      <div>
         <h1>Questionnaire 🧮</h1>
         <h2>Question {questionNumber} </h2>

         {questionNumberInt === 1 ? null : (
            <Link to={prevQuestionNumber}>Précédent</Link>
         )}

         {questionNumberInt === 10 ? (
            <Link to="/results">Résultats</Link>
         ) : (
            <Link to={nextQuestionNumber}>Suivant</Link>
         )}
      </div>
   );
}

export default Survey;

With this code I can observe (let assume that we are currently at page "/survey/2") that通过这段代码,我可以观察到(假设我们当前位于页面“/survey/2”)

<Link to={prevQuestionNumber}>Précédent</Link>

is tranformed in the HTML file into在HTML文件中转化为

<a href="/survey/2">Précédent</a>

(same for the next link which refers to "/survey/2"). (与下一个链接相同,它指的是“/survey/2”)。 So for whatever reason my links aren't working.因此,无论出于何种原因,我的链接都无法正常工作。

I've found that I can make it works by writing我发现我可以通过写作来让它工作

<Link to={'/survey/${prevQuestionNumber}'}>Précédent</Link>

and then I actually obtain prev and next links thats refers to "/survey/1" and "/survey/3".然后我实际上获得了指向“/survey/1”和“/survey/3”的 prev 和 next 链接。

But I'm annoyed by having to write the full url, as react router v6 introduced relative routes, so I want to use that.但是我很烦不得不写完整的 url,因为 react router v6 引入了相对路由,所以我想使用它。 Indeed, I've found that if I write事实上,我发现如果我写

<a href={prevQuestionNumber}>Précédent</a>

directly in my code, instead of the using Link , then it works perfectly and I'm correctly redirected when clicking my prev and next links.直接在我的代码中,而不是使用Link ,然后它就可以完美地工作,并且在单击我的上一个和下一个链接时我被正确重定向。

So I don't know how to recreate this using Link and why <Link to={prevQuestionNumber}>Précédent</Link> doesn't works, can someone explain me please?所以我不知道如何使用Link重新创建它以及为什么<Link to={prevQuestionNumber}>Précédent</Link>不起作用,有人可以解释一下吗?

Hey Welcome to Stack Overflow,嘿,欢迎来到 Stack Overflow,

So the reason why below code is working所以下面的代码有效的原因

<Link to={'/survey/${prevQuestionNumber}'}>Précédent</Link>

If you see your routes, you have only one route which accept dynamic path and that is如果你看到你的路线,你只有一条路线接受动态路径,那就是

<Route path="/survey/:questionNumber" element={<Survey />} />

On the other hand, when you are trying <Link to={prevQuestionNumber}>Précédent</Link> it does not find relavant path in the routes and gives error page or no page or blank page.另一方面,当您尝试<Link to={prevQuestionNumber}>Précédent</Link>时,它不会在路由中找到相关路径并给出错误页面或没有页面或空白页面。

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

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