【发布时间】:2021-09-13 09:57:44
【问题描述】:
错误信息
'Router' cannot be used as a JSX component.
Its return type 'void' is not a valid JSX element. TS2786
import App from './App';
5 |
> 6 | ReactDOM.render(<Router />, document.getElementById('root'));
index.tsx 代码
ReactDOM.render(<Router />, document.getElementById('root'));
Router.tsx 代码
import React from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import App from './App';
export default function Routes() {
<>
<BrowserRouter>
<Switch>
<Route exact path="/" component={App} />
</Switch>
</BrowserRouter>
</>;
}
tsconfig
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
},
"include": ["src"]
}
这是什么错误?
index.tsx 中的错误<Router />
我该如何解决这个问题?
【问题讨论】:
-
你没有从你的组件返回任何东西。
-
如果您只有一条路线,为什么还要使用
react-router?只需让App成为您的源组件。
标签: javascript reactjs typescript react-router