reactjs - React, dynamically change part of a route
问题描述
I have read up on dynamic routes, but haven't found a solution that worked for me. I am working on a project where, when I click next, I want the routes to change as follows:
/part1/4
/part1/3
/part1/2
/part1/1
/part2/4
/part2/3
/part2/2
/part2/1
…
/part5/4
/part5/3
/part5/2
/part5/1
And of course if I click back, it should back through the routes.
I have this in App.js:
<Route path='/:part/:score' exact component={Questionnaire} />
But I'm not sure what I would need to put into the "to" part to change the /part1/ to part2, 3, 4, et.c at the appropriate time.
<Link to={`/part1/${scoreNum}`} id="next" onClick={this.disablePrevSelection} className={`btn btn-normal btn-green ${disabled}`}>Next ></Link>
I apologize in advance if this is a simple question as I am new to ReactJs. Please let me know if anything else is needed.
Thank you!
解决方案
Your route should be like this:
<Route path='/path' component={Questionnaire} />
Note that we omitted exact here so that we can match every route starting with path
And link to it would be something like:
<Link to={`/part${partNum}/${scoreNum}`} id="next" onClick={this.disablePrevSelection} className={`btn btn-normal btn-green ${disabled}`}>Next ></Link>
推荐阅读
- java - ExecutorService - 来自调试和在 Eclipse 中运行的不同输出
- ms-access - 如何让 ms-access 保存标签设置?
- django - 如何获取上传文件的绝对路径?- Python + Django
- sql - 如何将一列性别分成男性和女性,然后在 SQL Server 中找到它们的总和?
- rest - Web 应用程序和 ReSTful 应用程序之间的区别?
- php - Instagram 基本显示 API 中的错误:未找到匹配代码或已使用匹配代码
- c# - 如何在 Redis 服务器流式传输上从 c# StackExchange.Redis nuget 包进行阻塞读取
- azure - 在流分析中添加以秒为单位的偏移时间
- python - swig 在 C++ 中运行的结果与在 python 中的不同
- html - 如何在表单中对齐保存按钮右侧