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.
The react-router-dom library supports inbuilt route-level code splitting. It allows us to download chunks at the route level. Using this feature, we'll code split at the route level, which is tremendously helpful.
EDITED: Multiple routed with multiple layouts can be done by using <Outlet> component in layouts and <Route> component composition.
The type information has no functionality of its own, so you need to install both. The type information is installed as a dev dependency because it isn't required after the build process which transforms typescript into JavaScript.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With