0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ESLint】【JSX】propsにスプレッド構文を使うのは好ましくない理由

Posted at

現在参画しているプロジェクトでは、ESLintのreact/jsx-props-no-spreadingを有効にし、propsにスプレッド構文を使うことは原則禁止となりました。

理由

↓のgithubの内容がわかりやすいですが、予期せぬ値が渡されてしまう可能性があるためです。また、渡したいプロパティを明示的に羅列するため、可読性の向上も見込めます。
https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md

良い例

渡す必要のある値のみ、明示的に記述する。

const {src, alt} = props;
const {one_prop, two_prop} = otherProps;
<MyCustomComponent one_prop={one_prop} two_prop={two_prop} />
<img src={src} alt={alt} />

悪い例

渡す必要のない値まで、スプレッドでまるっと全部渡してしまう

<App {...props} />
<MyCustomComponent {...props} some_other_prop={some_other_prop} />
<img {...props} />
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?