我可以在不使用状态或 redux 的情况下获取反应路由中的所有访问路径吗
can i get all visited path in react routing without using state or redux
我可以在不使用 state 或 redux 的情况下获取反应路由中所有访问过的路径吗
ex>> 我在 "ask" 的路由中
https://whosebug.com/questions/询问
如果我去另一条路
https://whosebug.com/questions/阅读
"read"
然后https://whosebug.com/questions/显示
我需要 git [询问,阅读,展示] 的列表而不使用状态或 redux 来存储它。(仅使用反应路线 属性/历史)
是否有支持此功能的库 React 路由?
是的,你可以。这种需求就用localStorage
吧。
import { browserHistory } from 'react-router';
//Your router initialization
browserHistory.listen( location => {
const previouslyVisitedPaths = localStorage.getItem("visitedPaths")
let paths
if (previouslyVisistedPaths === null) { // First write to your localStorage
paths = location
} else {
paths += `,${location}` // append new path to it
}
localStorage.setItem("visitedPaths", paths)
});
此代码将为您提供稍后访问的路径:
localStorage.getItem("visitedPaths").split(',')
我可以在不使用 state 或 redux 的情况下获取反应路由中所有访问过的路径吗
ex>> 我在 "ask" 的路由中 https://whosebug.com/questions/询问 如果我去另一条路 https://whosebug.com/questions/阅读
"read"
然后https://whosebug.com/questions/显示
我需要 git [询问,阅读,展示] 的列表而不使用状态或 redux 来存储它。(仅使用反应路线 属性/历史)
是否有支持此功能的库 React 路由?
是的,你可以。这种需求就用localStorage
吧。
import { browserHistory } from 'react-router';
//Your router initialization
browserHistory.listen( location => {
const previouslyVisitedPaths = localStorage.getItem("visitedPaths")
let paths
if (previouslyVisistedPaths === null) { // First write to your localStorage
paths = location
} else {
paths += `,${location}` // append new path to it
}
localStorage.setItem("visitedPaths", paths)
});
此代码将为您提供稍后访问的路径:
localStorage.getItem("visitedPaths").split(',')