[英]Dynamically create buttons in react native
我想根據 API 中的數據動態創建一個復選框,然后在我的render
函數中調用該函數。
任何人都可以以任何方式建議我,因為我曾經使用過forEach
但數據似乎沒有通過,因此我收到一個錯誤: forEach is undefined
。
我的片段:
sizel=()=> this.state.data.sizes.forEach(function(size){
<CheckBox
title={size}
checked=''
/>
})
檢查data.sizes
長度也不應該是未定義的。
添加這個:
const { sizes } = this.state.data;
if( sizes && sizes.length){
this.state.data.sizes.map(size => <CheckBox title={size} checked=''/> )
}
您需要在調用 forEach 方法之前添加驗證,就像這樣:
sizel = () => this.state.data && this.state.data.sizes && this.state.data.sizes.forEach(function (size) {
return (
<CheckBox
title={size}
checked=''
/>
)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.