簡體   English   中英

如何從 React.FC 組件返回 null

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM