Sijan Shrestha Sijan Shrestha - 1 year ago 89
React JSX Question

How to Route without reloading the whole page?

I am Using react-route, and i am facing some trouble with routing.

The whole page is reloading , causing all the data that i have already fetched and stored in the reducer to load every time.

Here is my Route file :

var CustomRoute = React.createClass({
return <Router history={browserHistory}>
<Route path="/" component={Layout}>
<IndexRedirect to="/landing" />
<Route path="landing" component={Landing} />
<Route path="login" component={Login} />
<Route path="form" component={Form} />
<Route path="*" component={NoMatch} />

Before routing i already store data by calling action in my main.jsx

/** @jsx React.DOM */
'use strict'
var ReactDOM = require('react-dom')
var React = require('react')
var Index = require('./components/index')
var createStore = require("redux").createStore
var applyMiddleware = require("redux").applyMiddleware
var Provider = require("react-redux").Provider
var thunkMiddleware = require("redux-thunk").default
var reducer = require("./reducers")
var injectTapEventPlugin =require('react-tap-event-plugin');
var createStoreWithMiddleware=applyMiddleware(thunkMiddleware)(createStore);
var store=createStoreWithMiddleware(reducer);
store.dispatch(actions.load_contacts()) //////////// <<<<<< this is what i call to store data already

ReactDOM.render( <Provider store={store}><Index/></Provider> , document.getElementById('content'))

The issue is i have to route to another page if sucess login :

if (!err){
window.location = "/form"

The window.location does the trick but it reloads everything which is very inefficient.
In manual route i use
that routes without reloading, however for automated i am not sure how to do it.

Any Suggestion will be much appreciated.

Answer Source

for the most recent release (v2.0.0-rc5), the recommended navigation method is by directly pushing onto the history singleton.


Relevant code

import { browserHistory } from './react-router'

If using the newer react-router API, you need to make use of the history from this.props when inside of components so:

static propTypes = {
    history: React.PropTypes.object


If using react-router-redux, it offers a push function you can dispatch like so:

import { push } from 'react-router-redux';

In v2.4.0 and above, use a higher order component to get the router as a prop of your component.

import { withRouter } from 'react-router'

class Example extends React.Component {
   // use `this.props.router.push('/some/path')` here

// Export the decorated class
var DecoratedExample = withRouter(Example);

// PropTypes

Example.propTypes = {
  router: React.PropTypes.shape({
    push: React.PropTypes.func.isRequired

Visit this link for more information:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download