LoginSignup
1
1

More than 3 years have passed since last update.

ReactのPropTypesで特定のキーを持つオブジェクトの型を定義して再利用する

Posted at

一箇所で使う場合は個別のコンポーネントに記載すれば良いが、別の場所で利用する場合に同じ内容を各コンポーネントで記述するのは冗長になる。

なので対象オブジェクトのPropTypesを定義したconstを定義しておき、利用すれば良い。

const hogePropTypes = PropTypes.shape({
  foo: PropTypes.string.isRequired,
  bar: PropTypes.bool
})

const HogeComponent = (props) => {(...)}

HogeComponent.propTypes = {
  piyo: PropTypes.string.isRequired,
  bar: hogePropTypes,
}

APIのModelから動的に生成してくれたりするライブラリないかな...

1
1
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
1
1