LoginSignup
5
2

More than 3 years have passed since last update.

[React]uniq key がないときの `Each child in a list should have a unique "key" prop.` の回避方法

Posted at

サンプル作ってたり、データにユニークな key がないときに出る Each child in a list should have a unique "key" prop. のエラーが面倒だなと思ってたんですが、 React.Children.toArray で回避できる。

<>
  {React.Children.toArray(items.map(item => <div>{item.title}</div>))}
</>

データ構造が非公開の children を平坦な配列として返し、それぞれの要素に key を割り当てます。

便利!

5
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
5
2