简体   繁体   English

反应路由器导航不起作用

[英]react-router navigation not working

I'm trying to navigate to a different route like this: 我正在尝试导航到这样的其他路线:

import React from 'react';
import { browserHistory } from 'react-router'

class MyComponent extends React.Component {

    navigate () {

        browserHistory.push('/some-page')

    }

    render () {
        return (            
            <button onClick={this.navigate.bind(this)} />Navigate</button>
        );
    }

}

export default MyComponent;

My routes setup: 我的路线设置:

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path='/' component={Layout}>
            <IndexRoute component={Home}></IndexRoute>
            <Route path='/some-page' component={SomePage}></Route>
        </Route>
    </Router>,
    document.getElementById('app')
);

When this runs the URL in the browser does indeed change, but that's about all that happens. 当它运行时,浏览器中的URL确实确实发生了变化,但这就是所有发生的情况。 I'm not actually getting directed to that route. 我实际上并没有直接去那条路线。 Is there something I'm missing? 有什么我想念的吗?

You need to be consistent; 您需要保持一致; you declared hashHistory for your router but using browserHistory for your actual routing. 您为路由器声明了hashHistory ,但对实际路由使用了browserHistory

Hash History will append the path after '#/', browserHistory use the newest HTML5 push state to actually change the URL without making any requests. 哈希历史记录将在'#/'后面追加路径,browserHistory使用最新的HTML5推送状态来实际更改URL,而不发出任何请求。

Each does what they are supposed, but you are listening to something and saying something else ;) 每个人都按照他们的预期去做,但是您正在听一些话并说其他话;)

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

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