react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Error: The root route must render a single element

Open Bboom1024 opened this issue 10 years ago • 16 comments

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

Bboom1024 avatar Dec 29 '15 06:12 Bboom1024

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.

taion avatar Dec 29 '15 06:12 taion

@Bboom1024 I have this issue too. What resolved it in the end?

jaydenseric avatar Jan 14 '16 04:01 jaydenseric

@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)
        })
      }
    },
...

oliviabanis avatar Feb 05 '16 23:02 oliviabanis

@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 :)

uynap avatar Feb 19 '16 10:02 uynap

Bable 5.x is deprecated. The example code should be updated to using Babel 6.x.

hexicle avatar Apr 04 '16 14:04 hexicle

There's no difference with respect to the code. And require.ensure is a webpack feature anyway.

taion avatar Apr 04 '16 14:04 taion

it's probably require('...').default, don't need require.ensure to update for babel 6.

ryanflorence avatar Apr 04 '16 23:04 ryanflorence

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)

rosskevin avatar May 13 '16 19:05 rosskevin

use
module.exports = Index instead of export default Index solve this

maybe don't support es6 in router

zxydaisy avatar Sep 29 '16 09:09 zxydaisy

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.

colshacol avatar Oct 05 '16 00:10 colshacol

@olgabanis thanks, I have the same problem, and change my App component from dynamic to static import, and then all going well

the1sky avatar Nov 04 '16 18:11 the1sky

@olgabanis thanks, so only the exports created using export default should be set to default. If using module.exports it works fine without.

chrisdengso avatar Dec 18 '16 16:12 chrisdengso

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

hitao123 avatar Jan 12 '17 15:01 hitao123

In router.js I changed the require paths:

from const Header = require('./HeaderComponent.jsx'); to: const Header = require('./HeaderComponent.jsx').default;

gretak avatar Jan 26 '17 10:01 gretak

@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.

hirokith avatar Feb 04 '17 15:02 hirokith

maybe such code can resolve it.

Component.__esModule ? Component.default : Component

V-Tom avatar Jun 07 '17 11:06 V-Tom