TypeError:无法读取未定义的属性“未定义”

麦地那兹

我有这个数组:

        super(props);
        this.state = {
            data: [{
                baseCurrency: 'GBP',
                baseAmount: 0,
                convertToCurrency: 'USD',
                rates: [],
                currencies: []
            },
                ]
        }

当我尝试在render()函数中访问它时,它给了我错误

const {currency,rates,baseCurrency,baseAmount,convertToCurrency} = this.state.data;

const result = Number.parseFloat(baseAmount * rates [convertToCurrency])。toFixed(2);

如果我不使用数组声明this.state,则不会发生此问题。

丹尼斯瓦什

您只能传播特定对象,因为它this.state.data是一个数组:

const [
  { currencies, rates, baseCurrency, baseAmount, convertToCurrency },
] = this.state.data;

并且更具可读性:

const [firstItem] = this.sate.data;
const {
  currencies,
  rates,
  baseCurrency,
  baseAmount,
  convertToCurrency,
} = firstItem;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法读取未定义的属性“ then”

“ TypeError:无法读取未定义的属性'then'

“ TypeError:无法读取未定义的属性”

如何修复'TypeError:无法读取未定义的属性'then'TypeError:无法读取未定义的属性'then'...'

TypeError:无法读取未定义的属性“数据”-但已定义

TypeError:无法读取未定义的属性(读取“pageNumber”)

React TypeError:无法读取未定义的属性(读取“状态”)

“TypeError:无法读取未定义的属性(读取'id')”

TypeError:无法读取未定义的属性(读取“有”)

ReactJS TypeError:无法读取未定义的属性(读取“地图”)

反应 - 错误:TypeError:无法读取未定义的属性(读取“then”)

TypeError:无法读取未定义的属性(读取“等于”)

React - TypeError:无法读取未定义的属性(读取“参数”)?

“TypeError:无法读取未定义的属性(读取'hasOwnProperty')”错误

刷卡器:TypeError:无法读取未定义的属性“ push”或TypeError:classNames未定义

无法读取未定义的属性(读取 *)

数组未定义:错误TypeError:无法读取未定义的属性“ push”

TypeError:无法读取未定义的属性“ id”(用户未定义)

Axios在React中未定义,错误:Uncaught TypeError:无法读取未定义的属性“ post”

× TypeError:无法读取未定义的属性“地图” - google.maps 未定义(React)

UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“body”

Formik-TypeError:无法读取未定义的属性'type'?

TypeError:无法读取未定义的属性“编译”

未捕获的TypeError:无法读取未定义的属性“值”

TypeError:无法读取未定义的属性“ map”

未捕获的TypeError:无法读取未定义的属性“ toLowerCase”

未捕获的TypeError:无法读取未定义的属性“ close”

Uncaught TypeError:无法读取未定义的属性“轴”

TypeScript:TypeError:无法读取未定义的属性“ push”