故障排除备忘录呼叫React+打字稿
我需要你的帮助。最近开始学习Typescript,遇到了一个React问题。 我尝试用 React.memo() 包装我的路由器。像这样:
export const Router = React.memo<RouterPropsInterface>(({ isAuth }) => {
if (!isAuth) {
return (
<Suspense fallback={<Loader text={""}/>}>
<Routes>
<Route path={'/'} element={<LoginPage/>}/>
<Route path={'*'} element={<LoginPage/>}/>
<Route path={'register'} element={<RegisterPage/>}/>
</Routes>
</Suspense>
)
}
return (
<Suspense fallback={<Loader text={""}/>}>
<Routes>
<Route path={'create'} element={<AddCard/>}/>
<Route path={'posts'} element={<UsersList/>}/>
<Route path={'/'} element={<Home/>}/>
<Route path={"*"} element={<Home/>}/>
</Routes>
</Suspense>
)
});
我的 RouterPropsInterface
看起来像这样:
export interface RouterPropsInterface {
isAuth: boolean,
}
但在这种情况下,我还有其他问题:
src/routers/Router.tsx
Line 12:23: Component definition is missing display name react/display-name
Line 12:59: 'isAuth' is missing in props validation react/prop-types
"react": "^17.0.2", "typescript": "^4.6.2"
我不想添加 eslint-disablers,只是想了解我做错了什么。
谢谢!
I need your help. Started learning Typescript recently and got one problem with React.
I try to wrap my Router with React.memo(). Smth like this:
export const Router = React.memo<RouterPropsInterface>(({ isAuth }) => {
if (!isAuth) {
return (
<Suspense fallback={<Loader text={""}/>}>
<Routes>
<Route path={'/'} element={<LoginPage/>}/>
<Route path={'*'} element={<LoginPage/>}/>
<Route path={'register'} element={<RegisterPage/>}/>
</Routes>
</Suspense>
)
}
return (
<Suspense fallback={<Loader text={""}/>}>
<Routes>
<Route path={'create'} element={<AddCard/>}/>
<Route path={'posts'} element={<UsersList/>}/>
<Route path={'/'} element={<Home/>}/>
<Route path={"*"} element={<Home/>}/>
</Routes>
</Suspense>
)
});
My RouterPropsInterface
looks like this:
export interface RouterPropsInterface {
isAuth: boolean,
}
But in that case, I have other problems:
src/routers/Router.tsx
Line 12:23: Component definition is missing display name react/display-name
Line 12:59: 'isAuth' is missing in props validation react/prop-types
"react": "^17.0.2",
"typescript": "^4.6.2"
I don't want to add eslint-disablers, just wanna understand what I'm doing wrong.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用
React.memo
包装组件时,React 不再推断displayName
属性。所以,你需要这样设置。关于 prop 类型,您可以通过添加 prop 类型来满足这一点,如下所示:
When wrapping a component with
React.memo
, thedisplayName
property is no longer inferred by React. So, you need to set it like so.Regarding prop types, you can satisfy this by adding prop types like so: