How to do a redirect to another route with react-router?

I am trying to do A SIMPLE using react-router ( version ^1.0.3 ) to redirect to another view and I am just getting tired.

import React from 'react';
import {Router, Route, Link, RouteHandler} from 'react-router';

class HomeSection extends React.Component {

  static contextTypes = {
    router: PropTypes.func.isRequired

  constructor(props, context) {
    super(props, context);

  handleClick = () => {
    console.log('HERE!', this.contextTypes);
    // this.context.location.transitionTo('login');

  render() {
    return (
        <Row className="text-center">          
          <Col md={12} xs={12}>
            <div className="input-group">
              <span className="input-group-btn">
                <button onClick={this.handleClick} type="button">

HomeSection.contextTypes = {
  location() {

export default HomeSection;

all I need is to send the use to '/login' and that's it.

What can I do ?

errors in console:

Uncaught ReferenceError: PropTypes is not defined

file with my routes

/*eslint-disable no-unused-vars*/
import React from 'react';
/*eslint-enable no-unused-vars*/
import {Route, IndexRoute} from 'react-router';

import Application from './components/App/App';
import Contact from './components/ContactSection/Contact';
import HomeSection from './components/HomeSection/HomeSection';
import NotFoundSection from './components/NotFoundSection/NotFoundSection';
import TodoSection from './components/TodoSection/TodoSection';
import LoginForm from './components/LoginForm/LoginForm';
import SignupForm from './components/SignupForm/SignupForm';

export default (
    <Route component={Application} path='/'>
      <IndexRoute component={HomeSection} />
      <Route component={HomeSection} path='home' />
      <Route component={TodoSection} path='todo' />
      <Route component={Contact} path='contact' />
      <Route component={LoginForm} path='login' />
      <Route component={SignupForm} path='signup' />
      <Route component={NotFoundSection} path='*' />

Answers 1

  • For the simple answer, you can use Link component from react-router, instead of button. There is ways to change the route in JS, but seems you don't need that here.

    <span className="input-group-btn">
      <Link to="/login" />Click to login</Link>

    To do it programmatically in 1.0.x, you do like this, inside your clickHandler function:

    this.history.pushState(null, 'login');

    Taken from upgrade doc here

    You should have this.history placed on your route handler component by react-router. If it child component beneath that mentioned in routes definition, you may need pass that down further

Related Articles