I want to create a checkbox dynamically according to the data in API and then call the function inside my render
function.
Can anyone suggest me any way, as I had used forEach
but the data seem do not passed and hence I'm getting an error: forEach is undefined
.
My snippet:
sizel=()=> this.state.data.sizes.forEach(function(size){
<CheckBox
title={size}
checked=''
/>
})
check data.sizes
has length also should not be undefined.
add this:
const { sizes } = this.state.data;
if( sizes && sizes.length){
this.state.data.sizes.map(size => <CheckBox title={size} checked=''/> )
}
You need to add a validation before invoking forEach method, just like this:
sizel = () => this.state.data && this.state.data.sizes && this.state.data.sizes.forEach(function (size) {
return (
<CheckBox
title={size}
checked=''
/>
)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.