[英]How can I return null from React.FC component
我對 ESLint 和 TypeScript 感到困惑。
我無法返回null
因為ESLint: Use undefined
而不是null
.(unicorn/no-null)
好的,我不介意將null
更改為undefined
。 現在我有更多錯誤
ESLint:不要使用無用的undefined
.(unicorn/no-useless-undefined)和
TS2322: 類型'({ testId, authType }: PropsWithChildren) => 元素 | undefined' 不可分配給類型 'FC'。 輸入'元素 | undefined' 不可分配給類型 'ReactElement<any, any> | 無效的'。 類型 'undefined' 不可分配給類型 'ReactElement<any, any> | 無效的'。
const AuthForm: React.FC<Props> = ({ testId = '', authType }) => {
switch (authType) {
case 'logIn':
return <LogIn testId={testId} />;
case 'changePass':
return <ChangePass testId={testId} />;
case 'newPass':
return <NewPass testId={testId} newPassStatus="resetPass" />;
default:
return null;
}
};
如何處理?
提前致謝!
我認為你應該關閉unicorn/no-null
規則,React 組件不能返回 undefined。
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/25546
{
"unicorn/no-null": "off",
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.