react-router - react 路由器 Prop `location`/ `match` 未更新为 `ConnectedRouter`

标签 react-router react-redux react-router-dom connected-react-router

我在文档中设置了我的应用程序:

第1步

...
import { createBrowserHistory } from 'history'
import { applyMiddleware, compose, createStore } from 'redux'
import { connectRouter, routerMiddleware } from 'connected-react-router'
...
const history = createBrowserHistory()

const store = createStore(
  connectRouter(history)(rootReducer), // new root reducer with router state
  initialState,
  compose(
    applyMiddleware(
      routerMiddleware(history), // for dispatching history actions
      // ... other middlewares ...
    ),
  ),
)

第2步
...
import { Provider } from 'react-redux'
import { Route, Switch } from 'react-router' // react-router v4
import { ConnectedRouter } from 'connected-react-router'
...
ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}> { /* place ConnectedRouter under Provider */ }
      <div> { /* your usual react-router v4 routing */ }
        <Switch>
          <Route exact path="/" render={() => (<div>Match</div>)} />
          <Route render={() => (<div>Miss</div>)} />
        </Switch>
      </div>
    </ConnectedRouter>
  </Provider>,
  document.getElementById('react-root')
)

我点击了 Link甚至 dispatch(push('/new-url/withparam'))
然而 Prop 为match location保留以前的值或第一页的任何内容。

怎么了?

最佳答案

这个已经咬了我很多次了。

您的 SwitchRoute等必须不是 在连接的组件中!

如果组件已连接,则 Prop 为 match , location等似乎没有更新并传播到您的路线。

这意味着不要连接您的顶级 AppRoot ,或 ConnectedRouter 之间的任何其他嵌套容器和 Route
——

更新:

您可能只需要用

<Route render={ (routerProps) => <YourConnectedComponent { ...routerProps } />

关于react-router - react 路由器 Prop `location`/ `match` 未更新为 `ConnectedRouter`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51903725/

相关文章:

reactjs - 当组件使用react-router-dom渲染时更改Url?

javascript - React Router 和 Redux - 初始加载数据

reactjs - 访问react-router动态段时崩溃

reactjs - 与 react 路由器 react : render component by replacing content in an element

javascript - 需要有关 React 路由器的帮助

javascript - Redux.js : catching malformed actions?

javascript - 每次渲染react时刷新组件状态

javascript - 使用 React 的嵌套列表

ios - react native : Yet another "Undefined is not an object (evaluating action. 类型)

reactjs - React/Redux - 将 this.props.history 传递给 thunk 是反模式吗?