I'm trying to upgrade a 2 year old react codebase to use the latest react and react-router. I haven't touched the webpack config at all, and I'm assuming I wouldn't need to. I'm new to the react world, so please bear with me. I have a few questions that I'll bundle into this one question, so it'll be fairly long.
- Bundles:
The webpack config loads a js file as an entry point.
var entry = { appBundle : ["./src/index.js"] }
and in index.js, we're doing something like:
var mainRoutes = {
component: "div",
childRoutes: [
{
path: "/",
component: require("./components/LandingPage.jsx"),
childRoutes: [
require("./components/dashboard"),
..
..
]
}
}
and then,
React.render(
React.createElement(Router, { history: browserHistory, routes: mainRoutes }),
document.getElementById("app-container")
);
I've kept the entry point same, but I've changed the content of index.js to something like:
ReactDOM.render((
<BrowserRouter>
<LandingPage />
</BrowserRouter>
), document.getElementById("app-container"));
and inside LandingPage.jsx, I've defined all top-level routes like so:
render() {
return (
<main>
<Switch>
<Route path='/dashboard' component={Dashboard}/>
..
</Switch>
</main>
);
}
The routing works fine and everything is ok. But the difference I see is that webpack used to emit something like:
Asset Size Chunks Chunk Names
14
appBundle.js xxx kB 0 appBundle
dashboard.js xxx kB 1 dashboard
.. and so on for most components
but now it's at:
Asset Size Chunks Chunk Names
1
appBundle.js xxx kB 0 appBundle
so Q1: am I missing some configurations for creating the bundles as before?
- getComponents
In each of our component folder, we have a index.js file, which has content like so:
"use strict";
module.exports = {
path: "dashboard",
getComponents(location, cb) {
require.ensure([], (require) => {
cb(null, require("./Dashboard.jsx"));
}, "dashboard");
}
};
I know getComponents isn't supported anymore in react router v4. Did this piece of code load the component asynchronously (and that created the different bundles?)?
Q2: What should replace getComponents and index.js files?
- Nested routes
React router 4 doesn't support nested routes, and I'm supposed to add the components wherever I need nested routes. But I'm not sure where. Say, our components look like so:
"use strict";
// import stuff
class DashboardContainer extends React.Component {
render() {
return (
<Dashboard {...this.props} />
);
}
}
class Dashboard extends React.Component {
render() {
return (
<div></div>
<SomeOtherComponent/>
);
}
}
export default DashboardContainer;
I changed this to (with all other code the same)
class Dashboard extends React.Component {
render() {
return (
<div>
<div></div>
<SomeOtherComponent/>
<Route path='/childComponent' component={ChildComponent}/>
</div>
);
}
}
but the child component does not render.
Q3: Where do I need to define the child/nested component route?
Let me know if there's any more information needed. Thanks.