LoginSignup
7
2

More than 1 year has passed since last update.

Component definition is missing display name を解決する

Posted at

forwardRef を実装中にeslintのエラーが出てしまいました。
スクリーンショット 2022-07-04 22.49.24.png

全てのコンポーネントに名前を付けることが推奨されています。
これはデバッグの際、問題を特定しやすいようにするためです。参考

今回は 名前がない と教えてくれていますので、名前を付与しましょう。

解決方法1

export const Textbox = forwardRef<
  HTMLInputElement,
  ComponentPropsWithoutRef<"input">>(({ className, ...props }, ref) => {
  return (
    <input
      type="text"
      {...props}
      ref={ref}
      className={clsx(className, styles.module)}
    />
  );
});

+ Textbox.displayName = 'Textbox'

解決方法2

アロー関数ではダメなので、関数名をつけるようにします。

export const Textbox = forwardRef<
  HTMLInputElement,
  ComponentPropsWithoutRef<"input">>
- (({ className, ...props }, ref) {
+ (function TextboxBase({ className, ...props }, ref) {
  return (
    <input
      type="text"
      {...props}
      ref={ref}
      className={clsx(className, styles.module)}
    />
  );
});

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2