A< Router />可能只有一个子元素

时间:2018-05-16 09:26:54

标签: javascript reactjs

< p>好吧,我做错了什么,据我所知,我的路由器只有一个子元素 - < / p> < p> mY routes.js组件如下 - < / p> < pre>< code>导入来自'react'的React 从'react-router-dom'导入{BrowserRouter as Router,Route}; 从“./MarketPlace”导入MarketPlace; 从“./Favourites”导入收藏夹; const routes =()=> {     回来(             <路由器>                     < DIV>             < Route exact path =“/”component = {MarketPlace} />                         < Route path =“/ Favorites”component = {Favorites} />         < / DIV>     < /路由器>      )     } 导出默认路由 < /代码>< /预> < p>我的index.js是这样的 - < / p> < pre>< code>从'react'导入React; 从'react-dom'导入ReactDOM; import'./index.css'; 从'./MarketPlace'导入MarketPlace; 从'./registerServiceWorker'导入registerServiceWorker; 从'react-dom'导入{render} 从“./routes.js”导入路线 ReactDOM.render(routes(),document.getElementById('root')); registerServiceWorker(); < /代码>< /预> < p>我正在使用最新版本的反应路由器,我已经安装了路由器dom。< / p>

1 个答案:

答案 0 :(得分:1)

使用ReactDOM.render渲染路径时,将其渲染为React组件而不是函数调用。另外,为了渲染组件,JSX标记需要以大写字符

开头
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import MarketPlace from './MarketPlace';
import registerServiceWorker from './registerServiceWorker';
import { render } from 'react-dom'
import Routes from "./routes.js"

ReactDOM.render(<Routes/>, document.getElementById('root'));
registerServiceWorker();