React Router道具`location` /`match`不会用ConnectedRouter`更新

ilo*_*ett 4 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 ...
    ),
  ),
)
Run Code Online (Sandbox Code Playgroud)

第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')
)
Run Code Online (Sandbox Code Playgroud)

我点击一个Link甚至dispatch(push('/new-url/withparam'))

但是,用于的道具match location将保留先前的值或第一页的内容。

怎么了?

ilo*_*ett 9

这个咬了我很多次。

SwitchRoute等一定不要在内部连接的组件!

如果组件连接,为道具matchlocation似乎等并没有得到更新,并传播到你的路线。

这表示请勿在和之间连接顶层AppRoot或任何其他嵌套容器ConnectedRouterRoute

-

更新:

您可能只需要用

<Route render={ (routerProps) => <YourConnectedComponent { ...routerProps } />
Run Code Online (Sandbox Code Playgroud)