joy
joy

Reputation: 3707

react router code in separate file

I am new to reactjs and working to learn redux with react router. I want to have routes as separate file. However somehow its not working because I am not able to pass the dependencies. Following is code details.

Working index.js having routes as well:

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from 'react-redux';
import {ReduxRouter} from "redux-react-router";
import {Route} from "react-router"

import createBrowserHistory from "history/lib/createBrowserHistory"
import configureStore from './store';

import App from "./containers/App";
import Home from "./components/Home";
import Countries from "./components/Countries";
import {fetchData} from './actions/actions';

const history = new createBrowserHistory();
const store = configureStore();

function loadData() {
  store.dispatch(fetchData('https://restcountries.eu/rest/v1/all'))
}

ReactDOM.render(
  <Provider store={store}>
    <ReduxRouter>
      <Route history={history}>
        <Route component={App}>
          <Route path='/' component={Home} />
          <Route path='/countries' component={Countries} onEnter={loadData} />
        </Route>
      </Route>
    </ReduxRouter>
  </Provider>,
  document.getElementById('root')
);

Following is the code which I tried to split as separate routes.js

index.js

import 'babel-core/polyfill';
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from 'react-redux';
import {ReduxRouter} from "redux-react-router";
import {Route} from "react-router"

import createBrowserHistory from "history/lib/createBrowserHistory"
import configureStore from './store';

import routes from "./routes";
import {fetchData} from './actions/actions';

const history = new createBrowserHistory();
const store = configureStore();

ReactDOM.render(
  <Provider store={store}>
    <ReduxRouter>
      <Route history={history} routes={routes}>        
      </Route>
    </ReduxRouter>
  </Provider>,
  document.getElementById('root')
);

routes.js

import React from "react";
import { Provider } from 'react-redux';
import {ReduxRouter} from "redux-react-router";
import {Route} from "react-router"


import App from "./../containers/App";
import Home from "./../components/Home";
import Countries from "./../components/Countries";

const router =
<Route history={history}>
  <Route component={App}>
    <Route path='/' component={Home} />
    <Route path='/countries' component={Countries} onEnter={loadData} />
  </Route>
</Route>;

export default router;

However it is throwing error as not able to identify loadData function.

Please help.

Upvotes: 7

Views: 11224

Answers (2)

joy
joy

Reputation: 3707

After following the answer given by Dominic I changed my code as below to pass the dependency as function argument as I don't want to create the store object again. Now its working fine. Following is modified code.

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from 'react-redux';
import {ReduxRouter} from "redux-react-router";
import {Route} from "react-router"

import createBrowserHistory from "history/lib/createBrowserHistory"
import configureStore from './store';

import App from "./containers/App";
import Home from "./components/Home";
import Countries from "./components/Countries";
import {fetchData} from './actions/actions';
import routes from "./routes";

const history = new createBrowserHistory();
const store = configureStore();

function loadData() {
  store.dispatch(fetchData('https://restcountries.eu/rest/v1/all'))
}


ReactDOM.render(
  <Provider store={store}>
    <ReduxRouter>
      <Route history={history}>
        {routes(loadData)}
      </Route>
    </ReduxRouter>
  </Provider>,
  document.getElementById('root')
); 

routes.js

import React from "react";
import { Provider } from 'react-redux';
import {ReduxRouter} from "redux-react-router";
import {Route} from "react-router"


import App from "./../containers/App";
import Home from "./../components/Home";
import Countries from "./../components/Countries";

function router(loadData) {
  return (<Route component={App}>
          <Route path='/' component={Home} />
          <Route path='/countries' component={Countries} onEnter={loadData} />
        </Route>);
}
export default router;

Upvotes: 3

nanobar
nanobar

Reputation: 66355

Pass it as a child, note the parent is called Router:

<Router history={history}>
  {routes}        
</Router>

Also Route doesn't take the history prop, Router does.

See an example of Router.js and Route.js on a starter of mine: https://github.com/DominicTobias/universal-react/tree/master/app

Upvotes: 14

Related Questions