首页 > 解决方案 > 哈希历史不能 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

标签: reactjsreact-router

解决方案


通过 Route 导航传递的状态值不会在浏览器刷新时保持不变,也不会在 URL 中考虑。处理这种情况的更好方法是使用查询参数而不是状态。所以你的网址看起来像/question?by=me/question?by=other

你可以像这样传递它

<Link to ={{
    pathname: "/questions", 
    search: '?by=others'
   }} >
   others
</Link>

并从中获取locationthis.props.location.search可以使用queryStringpackage 或类似工具解析的内容


推荐阅读