Skip to content

Instantly share code, notes, and snippets.

@supnate
Created March 16, 2018 07:52
Show Gist options
  • Save supnate/a88e8f654d08f463b23c39a5c629c130 to your computer and use it in GitHub Desktop.
Save supnate/a88e8f654d08f463b23c39a5c629c130 to your computer and use it in GitHub Desktop.
Rekit: src/Root.js
/* This is the Root component mainly initializes Redux and React Router. */
import React from 'react';
import PropTypes from 'prop-types';
import { Provider } from 'react-redux';
import { Switch, Route } from 'react-router-dom';
import { ConnectedRouter } from 'react-router-redux';
import history from './common/history';
function renderRouteConfigV3(routes, contextPath) {
// Resolve route config object in React Router v3.
const children = []; // children component list
const renderRoute = (item, routeContextPath) => {
let newContextPath;
if (/^\//.test(item.path)) {
newContextPath = item.path;
} else {
newContextPath = `${routeContextPath}/${item.path}`;
}
newContextPath = newContextPath.replace(/\/+/g, '/');
if (item.component && item.childRoutes) {
const childRoutes = renderRouteConfigV3(item.childRoutes, newContextPath);
children.push(
<Route
key={newContextPath}
render={props => <item.component {...props}>{childRoutes}</item.component>}
path={newContextPath}
/>
);
} else if (item.component) {
children.push(<Route key={newContextPath} component={item.component} path={newContextPath} exact />);
} else if (item.childRoutes) {
item.childRoutes.forEach(r => renderRoute(r, newContextPath));
}
};
routes.forEach(item => renderRoute(item, contextPath));
// Use Switch so that only the first matched route is rendered.
return <Switch>{children}</Switch>;
}
export default class Root extends React.Component {
static propTypes = {
store: PropTypes.object.isRequired,
routeConfig: PropTypes.array.isRequired,
};
render() {
const children = renderRouteConfigV3(this.props.routeConfig, '/');
return (
<Provider store={this.props.store}>
<ConnectedRouter history={history}>{children}</ConnectedRouter>
</Provider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment