我发现在不使用完成react-router-redux路由的情况下解决此问题的唯一工作方法action creator async action是将historyprops从组件传递给动作创建者并执行以下操作:
history.push('routename');
由于BrowserRouter忽略了传递给它的 history 属性,因此我们不能使用带有 browserhistory 的自定义历史对象。
解决这个问题的最佳方法是什么?
我发现在不使用完成react-router-redux路由的情况下解决此问题的唯一工作方法action creator async action是将historyprops从组件传递给动作创建者并执行以下操作:
history.push('routename');
由于BrowserRouter忽略了传递给它的 history 属性,因此我们不能使用带有 browserhistory 的自定义历史对象。
解决这个问题的最佳方法是什么?
BrowserRouter您可以使用Router自定义历史记录,而不是使用
import { Router } from 'react-router'
import createBrowserHistory from 'history/createBrowserHistory'
export const history = createBrowserHistory()
<Router history={history}>
<App/>
</Router>
在这种情况下,您history.push()将工作。使用 BrowserRouterhistory.push不起作用,因为创建新的browserHistory不起作用,因为<BrowserRouter>创建了自己的历史实例,并侦听了更改。因此,不同的实例将更改 url 但不会更新<BrowserRouter>.
创建自定义后history,您可以将其导出,然后将其导入您的action creator并使用它来动态导航,例如
import { history } from '/path/to/index';
const someAction = () => {
return dispatch => {
ApiCall().then((res) => {
dispatch({type: 'SOME_CALL', payload: res })
history.push('/home');
})
}
}