umijs / umi

A framework in react community ✨
https://umijs.org
MIT License
15.3k stars 2.65k forks source link

请问 umi 是如何实现 dva 和 react-router-dom 兼容的? #5714

Closed imhxc closed 3 years ago

imhxc commented 3 years ago

What happens?

众所周知,在 dva 中,通过如下方式可以配置 history

import dva from 'dva';
const app = dva({
  history: createHistory() 
});
app.router(() => <p>1</p>);
app.start('#root');

同时,在 react-router-dom v5 版本中,history 的获取只能通过 useHistory 获得:

import { useHistory } from 'umi';
const App = () => {
  const history = useHistory(); // 注意该 hooks 必须在路由的上下文中调用,即这里我们假设了 App 组件已经是一个路由组件
  return <div></div>
}

所以,如何在实例化的时候,就能将 useHistory 获取的 history 对象传递给 dva?

umi 如何实现的?

我看了 umi 的源码,发现 useHistory 就是直接导出的 react-router-dom v5 中的 hooks,而且 dva 也是用的目前版本的 dva。

所以,想问一下,umi 是如何实现的?

github-actions[bot] commented 3 years ago

Hello @imhxc, we use GitHub issues to trace bugs or discuss plans of Umi. So, please don't ask usage questions here. You can try to open a new discussion in umi discussions, select Q&A to ask questions.

你好 @imhxc,Umi Issue 板块是用于 bug 反馈与需求讨论的地方。请勿询问如何使用的问题,你可以试着在 umi discussions 新开一个 discussion,选择 Q&A 类别进行提问。

sorrycc commented 3 years ago

umi 使用 dva 时的 history 是传入的,和传给 react-router 的是同一个。