问题描述
在 react-router v5 中,我创建了这样的历史对象:
import { createBrowserHistory } from history";导出 const 历史记录 = createBrowserHistory();
然后将其传递给路由器:
import { Router, Switch, Route, Link } from react-router-dom";<路由器历史={历史}>...我的路线</路由器>
我这样做是为了有机会了解组件之外的使用历史:
//存储动作登出() {this.user = null;history.push('/');}
通过这种方式,我将逻辑移到了商店,并且组件尽可能保持干净.但是现在,在 react router v6 中我不能这样做.我仍然可以在我的组件中使用 useNavigate()
进行导航,但是我无法创建一个 navigate
来使用它进入我的商店.有什么办法吗?
好吧,事实证明,如果您实现一个自定义路由器,以与RRDv6 路由器.
检查
In react-router v5 i created history object like this:
import { createBrowserHistory } from "history";
export const history = createBrowserHistory();
And then passed it to the Router:
import { Router, Switch, Route, Link } from "react-router-dom";
<Router history={history}>
... my routes
</Router>
I did it for the opportunity to usage history outside of component:
// store action
logout() {
this.user = null;
history.push('/');
}
This way I moved the logic to the store and the components were kept as clean as possible. But now, in react router v6 i cant do the same. I can still navigate using useNavigate()
inside my component, but i cannot create a navigate
to use its into my store. Is there any alternative?
Well, it turns out you can duplicate the behavior if you implement a custom router that instantiates the history state in the same manner as RRDv6 routers.
Examine the BrowserRouter implementation for example:
Create a CustomRouter
that consumes a custom history
object and manages the state:
const CustomRouter = ({ history, ...props }) => {
const [state, setState] = useState({
action: history.action,
location: history.location
});
useLayoutEffect(() => history.listen(setState), [history]);
return (
<Router
{...props}
location={state.location}
navigationType={state.action}
navigator={history}
/>
);
};
This effectively proxies the custom history
object into the Router
and manages the navigation state.
From here you swap in the CustomRouter
with custom history
object for the existing Router
imported from react-router-dom
.
export default function App() {
return (
<CustomRouter history={history}>
<div className="App">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/profile" element={<Profile />} />
</Routes>
</div>
</CustomRouter>
);
}
Fork of your codesandbox:
这篇关于react router v6 在组件之外导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!