gpt4 book ai didi

reactjs - React Route 组件上的类型错误

转载 作者:搜寻专家 更新时间:2023-10-30 21:37:50 26 4
gpt4 key购买 nike

我正在尝试使用路由 Route 组件,但它抛出了以下错误:

[ts] Type '{ path: "/:shortname"; component: typeof FirstComponent; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{ children?: ReactNode; }> & Rea...'.

这是我的路线配置:

import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

<Router>
<Link to='/customer'></Link>
<Route path='/:shortname' component={FirstComponent} />
</Router>

这些是我正在使用的版本:

"@types/react-router-dom": "^4.0.5"
"react-router-dom": "^4.1.1"

最佳答案

好吧,问题是<Router/>元素只接受单例 child 。而你的 children for Router 是一个 Link & Route 的数组。

尝试用 div 或更高级的 React.Fragment 包装您的链接和路由( react 16.2+ 后)

<Router>
<React.Fragment>
<Link to='/customer'></Link>
<Route path='/:shortname' component={FirstComponent} />
</React.Fragment>
</Router>

关于reactjs - React Route 组件上的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48333400/

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