[英]undefined is not a function (near '...this.state.dataBanner.map...')
I have already checked some similar questions with the same problem: ReactJS TypeError: undefined is not a function (near '...this.state.data.map...') I have already checked some similar questions with the same problem: ReactJS TypeError: undefined is not a function (near '...this.state.data.map...')
I have followed each of the steps: dataBanner:[]
and dataBanner:responseJson
我已按照每个步骤操作: dataBanner:[]
和dataBanner:responseJson
...
import Swiper from 'react-native-swiper'
...
constructor(props){
super(props);
this.state = {
dataNews:[],
dataBanner:[]
}
}
componentDidMount(){
return fetch(baseUrl)
.then((response)=> response.json())
.then((responseJson)=>{
console.log(responseJson)
this.setState({
dataNews:responseJson,
dataBanner:responseJson
})
//console.log(responseJson)
})
.catch((error)=>{
console.log(error)
})
}
But the following code still gives me an error:但是下面的代码仍然给我一个错误:
<Swiper>
{this.state.dataBanner.map((itemImg)=>{
return (
<Image
source ={{ uri: itemImg.urlToImage }}
style ={{height:"100%", width:"100%"}}
/>
)
})}
</Swiper>
You can try你可以试试
this.setState({
dataNews:responseJson?.articles, // add articals here
dataBanner:responseJson?.articles // add articals here
})
and also you can validate你也可以验证
<Swiper>
{this.state.dataBanner?.length > 0 && this.state.dataBanner.map((itemImg) => {
return (
<Image
source={{ uri: itemImg.urlToImage }}
style={{ height: "100%", width: "100%" }}
/>
)
})}
</Swiper>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.