gpt4 book ai didi

reactjs - 不变违规 : The root route must render a single element error in react-router 2 dynamic routing

转载 作者:行者123 更新时间:2023-12-03 13:26:03 24 4
gpt4 key购买 nike

我有简单的 Hello World 应用程序,其中只有一条路线,没有子路线或索引路线。对于路由我使用 plain routes而不是 jsx sysntax。我再次使用react-router的dynamic routing使用 webpack 加载 Hello 组件。我的 app.jsx 文件包含以下代码

import React from "react";
import ReactDOM from "react-dom";
import {Router, browserHistory} from "react-router";
import Hello from "./components/Hello";



const routes = [{
path:"/",
getComponents(location, callback) {
require.ensure([], function (require) {
callback(null, require('./components/Hello'))
})
}
}];


ReactDOM.render(
<Router history={browserHistory} routes={routes}/>,
document.getElementById("container")
);

Hello.jsx 组件具有以下代码

import React from "react";
export default class Hello extends React.Component {

render() {

return (
<h2>Hello World</h2>
)
}
}

最佳答案

出现此错误是因为 webpack 不支持 es6 模块

如果您使用 babel 来转译 es6 代码,请使用默认关键字,例如

require('./components/Hello').default

所以路线将是

const routes = [{
path:"/",
getComponents(location, callback) {
require.ensure([], function (require) {
callback(null, require('./components/Hello').default)
})
}
}];

关于reactjs - 不变违规 : The root route must render a single element error in react-router 2 dynamic routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com