简体   繁体   English

匹配路由不会改变路由散列更改

[英]Matched route not changing on routing hash change

I'm using react-router-dom with react-router-redux and history to manage routing for my app. 我正在使用react-router-domreact-router-redux以及history来管理我的应用程序的路由。 I'm also using hash history for support on legacy browsers. 我也使用哈希历史记录来支持旧版浏览器。 Below are my route components: 以下是我的路线组件:

<Switch>
    <Route exact path={'/'} component={...} />
    <Route path={'/a'} component={...} />
    <Route path={'/b'} component={...} />
</Switch>

My app lands at the location: http://something.com/index.html#/ , and correctly is routed to the first Route component. 我的应用程序位于以下位置: http://something.com/index.html#/http://something.com/index.html#/ ,并正确路由到第一个Route组件。 However, when using dispatch(push('/a')) in a thunk action creator to attempt to programatically switch routes, I'm finding that the proper route is not being matched. 但是,当在thunk动作创建器中使用dispatch(push('/a'))尝试以编程方式切换路由时,我发现正确的路由没有匹配。

I'm having a difficult time debugging this... any ideas? 我很难调试这个......任何想法? I'm thinking it perhaps has to do with the fact that my window.location.pathname is /index.html . 我想它可能与我的window.location.pathname/index.html的事实有关。

Switch receive a location prop, or must be wrapped with Router component. Switch接收一个位置prop,或者必须用Router组件包装。 You can find more information at https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Switch.md#children-node/ 您可以在https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Switch.md#children-node/找到更多信息。

If a location prop is given to the , it will override the location prop on the matching child element. 如果给定位置道具,它将覆盖匹配的子元素上的位置道具。

So try one of these ways: 所以尝试以下方法之一:

 class Example extends Component { render() { return ( <Switch location={this.props.location}> <Route exact path={'/'} component={...} /> <Route path={'/a'} component={...} /> <Route path={'/b'} component={...} /> </Switch> ); } // location is react-router-redux reducer export default connect(state => ({location: state.location}))(Example); 

Or, another way you can do, it's wrap your Switch component with Router component (I pasted code from one of my project): 或者,您可以采用另一种方式,将Switch组件与路由器组件包装在一起(我从我的一个项目中粘贴了代码):

 import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { Route, Switch } from 'react-router-dom'; import { ConnectedRouter } from 'react-router-redux'; import createHistory from 'history/createBrowserHistory'; import configureStore from './store/configureStore'; const history = createHistory(); const store = configureStore(history); // We wrap Switch component with ConnectedRouter: ReactDOM.render( <Provider store={store}> <ConnectedRouter history={history}> <Switch> <Route exact path={'/'} component={...} /> <Route path={'/a'} component={...} /> <Route path={'/b'} component={...} /> </Switch> </ConnectedRouter> </Provider>, document.getElementById('root') ); 

More information about Router components you can find here: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Router.md 有关Router组件的更多信息,请访问: https//github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Router.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM