Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-router browserHistory.push doesn't redirect

Trying to use browserHistory.push method to change my route programmatically.

It will change the route (per browser address bar) but doesn't update the view.

Here's my code App.jsx

const AppStart = React.createClass({

  render: function() {
    return (
      <MuiThemeProvider>
        <Router history={hashHistory}>
          <Route path="/" component={Main}>
            <Route path="experiences" component={Experiences} />
            <Route path="people" component={Profiles} />
            <Route path="login" component={Login} />
            <IndexRoute component={Home}/>
          </Route>
        </Router>
      </MuiThemeProvider>
    );
  }
});

ReactDOM.render(
  <AppStart />,
  document.getElementById('app')
);

Component:

handleLoginRedirect(e) {
    browserHistory.push('/experiences');
  },

  render() {
    return (
      <div className='row'>
        <div className='col-sm-10 col-sm-offset-1'>
          <form role='form'>
           <RaisedButton label="Redirect" onClick={this.handleLoginRedirect} />
          </form>
        </div>
      </div>
    );
like image 612
zsayn Avatar asked Mar 12 '23 10:03

zsayn


1 Answers

Your router configuration uses hashHistory while you're pushing onto browserHistory.

It's easy to miss something like this and it's understandable.

  • Replace hashHistory with browserHistory in your Router like so:

<Router history={browserHistory}>

Full Snippet:

const AppStart = React.createClass({

  render: function() {
    return (
      <MuiThemeProvider>
        <Router history={browserHistory}>
          <Route path="/" component={Main}>
            <Route path="experiences" component={Experiences} />
            <Route path="people" component={Profiles} />
            <Route path="login" component={Login} />
            <IndexRoute component={Home}/>
          </Route>
        </Router>
      </MuiThemeProvider>
    );
  }
});
like image 134
KumarM Avatar answered Mar 16 '23 04:03

KumarM