LoginSignup
2
0

More than 5 years have passed since last update.

現象

Object.valuesを使った場合、返り値がArray<mixed>だとflowに認識されてしまいます。

const obj = {
  data: {
    title: 'hoge',
    label: 'fuga',
  },
}

const fromValue = Object.values(obj).map(data => console.log(data.title)); // fails

解決法

Object.keysを使用するとでエラーを回避できます

const fromKeys = Object.keys(obj).map(key => console.log(obj[key].title)); // works

Try Flowで実際に動かしてみてください)

reactで以下の様にObject.valuesを使うときは、

  {Object.values(obj).map((data) => (
    <CustomContainer
      title={data.title}
      label={data.label}
    >
  )}

Object.keysに置き換えたらFlowエラーがでなくなります。

  {Object.keys(obj).map((key) => (
    <CustomContainer
      title={obj[key].title}
      label={obj[key].label}
    >
  )}

参考資料

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