Error: The root route must render a single element
when I use the “auth-with-shared-root"of example to build my project.
This is my code of app.js
import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router } from 'react-router'
import routes from './config/routes'
render(<Router history={browserHistory} routes={routes}></Router>, document.getElementById('example'));
This is my code of index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>react-router</title>
<meta charset="utf-8">
<link rel="alternate" href="app.css">
<link rel="stylesheet" href="global.css" />
</head>
<body>
<div id="example"></div>
<script src="/build/js/bundle.js"></script>
</body>
</html>
I open chrome and It report Error Uncaught Invariant Violation: The root route must render a single element
Thanks for your question!
We want to make sure that the GitHub issue tracker remains the best place to track bug reports and feature requests that affect the development of React Router.
Questions like yours deserve a purpose-built Q&A forum. Could you post this question to Stack Overflow with the tag #react-router? https://stackoverflow.com/questions/ask?tags=react-router.
We also have an active and helpful React Router community on Reactiflux, which is a great place to get fast help with React Router and with the rest of the React ecosystem. You can join at https://discord.gg/0ZcbPKXt5bYaNQ46.
@Bboom1024 I have this issue too. What resolved it in the end?
@Bboom1024 @jaydenseric Not sure if you've figured it out already, but I got around it by importing the components and using those in the callbacks.
import App from '../components/App';
import Logout from '../components/Logout';
export default {
component: App,
childRoutes: [
{ path: '/logout',
getComponent: (location, cb) => {
require.ensure([], (require) => {
cb(null, Logout)
})
}
},
...
@olgabanis It's the difference between babel 5.x and 6.x. I can pack the project with babel 5.x without any modification. But for babel ^6.5, you have to import all components at the start of the file.
Thank you for sharing the solution. It works anyway :)
Bable 5.x is deprecated. The example code should be updated to using Babel 6.x.
There's no difference with respect to the code. And require.ensure is a webpack feature anyway.
it's probably require('...').default, don't need require.ensure to update for babel 6.
So more specifically for the novices like me using this example with babel 6+ convert all:
require('') statements in routes.js to require('').default
To be very clear:
cb(null, require('../components/Landing')) -> cb(null, require('../components/Landing').default)
use
module.exports = Index instead of
export default Index
solve this
maybe don't support es6 in router
Just wanted to chime in and say that I just had this issue and realized that I forgot to export.
If you're experiencing this, be sure to check all of your components and make sure they are exported.
@olgabanis thanks, I have the same problem, and change my App component from dynamic to static import, and then all going well
@olgabanis thanks, so only the exports created using export default should be set to default. If using module.exports it works fine without.
const routes = {
childRoutes: [{
path: '/',
component: require('../containers/App'),
indexRoute : {
getComponent(nextState, cb) {
require.ensure([], (require) => {
cb(null, require('../components/Home'))
}, 'Home')
}
},
childRoutes: [
require('../routes/All'),
require('../routes/Archives'),
require('../routes/Articles'),
require('../routes/Tags')
]
}]
}
export default routes
then the browser tell me The root route must render a single element, i want to know how to orgnize the strucure
In router.js I changed the require paths:
from
const Header = require('./HeaderComponent.jsx');
to:
const Header = require('./HeaderComponent.jsx').default;
@all, for webpack 2 I've found an article described how to make it work: http://moduscreate.com/code-splitting-for-react-router-with-es6-imports/, FYI.
maybe such code can resolve it.
Component.__esModule ? Component.default : Component