我正在尝试理解
文档说:
我正在尝试在thunk中执行
问题:
如果我导入
react-router-dom
(v5)包中的BrowserRouter
和Router
之间的区别,以及它对下面的示例有何影响。文档说:
来源: https://reacttraining.com/react-router/web/api/BrowserRouterBrowserRouter 使用HTML5历史API(pushState、replaceState和popstate事件)来使你的UI与URL同步。
来源: https://reacttraining.com/react-router/web/api/Router 据我所知,我应该在我的HTML5浏览器应用程序中使用Router 所有路由器组件的常见低级接口。通常应用程序将使用其中一个高级路由器:BrowserRouter、HashRouter、MemoryRouter、NativeRouter、StaticRouter。
BrowserRouter
,到目前为止我一直这样做。
history.push(...)
示例:我正在尝试在thunk中执行
history.push('/myNewRoute')
:import history as './history';
...
export function someAsyncAction(input) {
return dispatch => {
fetch(`${API_URL}/someUrl`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ input }),
}).then(() => {
history.push('/myNewRoute');
}).catch((err) => {
dispatch(setError(err));
})
};
};
history
被定义为这个模块:
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
并且{{history}}也传递给了我的路由器:
import { BrowserRouter as Router } from 'react-router-dom';
import history as './history';
...
const App = () => (
<Router history={history}>
...
</Router>
);
问题:
history.push()
会更新浏览器地址栏中的URL,但不会渲染路由 behind the route。如果我导入
Router
而不是BrowserRouter
,它可以工作:// Does not work:
import { BrowserRouter as Router } from 'react-router-dom';
// Does work:
import { Router } from 'react-router-dom';
BrowserRouter
相对于Router
自动处理了什么? - user3711421