reactjs - 哈希历史不能 PUSH 相同的路径;当我更改状态而不是路径名时,不会将新条目添加到历史堆栈中
问题描述
我只想改变状态而不是路线
<Link to ={{
pathname: "/questions",
state: {
by: 'me'
}
}} >
Me
</Link>
到
<Link to ={{
pathname: "/questions",
state: {
by: 'others'
}
}} >
others
</Link>
状态没有改变并且在控制台抛出
Warning: Hash history cannot PUSH the same path; a new entry will not be added to the history stack
解决方案
通过 Route 导航传递的状态值不会在浏览器刷新时保持不变,也不会在 URL 中考虑。处理这种情况的更好方法是使用查询参数而不是状态。所以你的网址看起来像/question?by=me
或/question?by=other
你可以像这样传递它
<Link to ={{
pathname: "/questions",
search: '?by=others'
}} >
others
</Link>
并从中获取location
您this.props.location.search
可以使用queryString
package 或类似工具解析的内容
推荐阅读
- python - 我应该多久在我的 python 项目中运行 requirements.txt 文件?
- c++ - 使用了 .ignore('\n') 并且后面的 Getline 函数只从文件中获取一半的输入
- java - Quartz 集群 - @DisallowConcurrentExecution 避免跨实例并行运行
- visual-c++ - 是否有使用 Visual C++ 的“运行”按钮的代码?
- javascript - 在最后一次出现斜线后匹配文本,包括斜线
- laravel - 将链接添加到 Laravel Auth Navbar
- java - Anaconda Tabula INFO:在旧的 java 1.8 或 9 版本上获得更高的渲染速度
- javascript - Grunt:从外部 YAML 文件访问序列/列表数据
- node.js - Docker Compose...使用错误的节点版本构建(以及其他问题)
- css - 手写笔错误:“预期为“缩进”,得到“缩进”,可能是因为插值无效