如何识别路线是通过history.push还是手动更改地址发起的(react-router) [英] How to recognize if the route was initiated through a history.push or a manual adress change (react-router)
问题描述
我将浏览器缓存作为真理的中心单一来源。
I have the browser cache as the central single source of truth.
当我想更改应用程序状态时,只需更改Broser缓存并调用setState即可触发render()。
When I want to change the app state, I just change the broser cache and call setState to trigger a render().
一切正常,除非用户手动更改地址(刷新窗口,单击书签等),以解决此问题,我制作了路由器的render方法(<路由器render = {render}>
)也将URL的参数 myUrl /:myVar1 /:myVar2
保存在浏览器缓存中
Everything works fine unless the user changes the adress manually (refreshes the window, clicks on a bookmark, etc...), to fix that, I made the router's render method (<Router render={render}>
) save the url's params myUrl/:myVar1/:myVar2
in the browser cache too
我的问题是,除非路由器被手动调用(例如,粘贴网址),否则我不希望路由器忽略缓存中的内容。有没有办法识别URL更改的起源(如果它来自history.push或其他操作)?
My issue is that I don't want the router to ovveride what is in the cache unless it was called manually (eg. by pasting a url). Is there a way to recognize the origin of the url change (if it comes from a history.push or another action)?
推荐答案
我不确定是否可以提供帮助。但是,如果您要寻找的是我,我会分享我的工作。
I am not entirely sure if I can help. But I'll share what I do in case its what you're looking for.
这是我保留最后一个路径名的方法,以便可以使用后退按钮
This is what I do to keep a hold of the last pathname so that I can have a back button
import React, { PureComponent } from "react";
import { withRouter } from "react-router-dom";
class Navigation extends PureComponent {
jump = path => {
this.props.history.push(this.props.history.location.pathname);
this.props.history.replace(path);
};
render() {
}
}
export default withRouter(Navigation);
with路由器包允许我们将窗口历史记录对象注入任何组件中
The with router package allows us to inject the window history object into any component
让我知道是否有更多查询
Let me know if any more queries
Daniel
这篇关于如何识别路线是通过history.push还是手动更改地址发起的(react-router)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!