我已经用同样的问题检查了一些类似的问题:ReactJS TypeError: undefined is not a function (near '...this.state.data.map...')
我遵循了每一个步骤: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)
})
}但是下面的代码仍然给出了一个错误:
<Swiper>
{this.state.dataBanner.map((itemImg)=>{
return (
<Image
source ={{ uri: itemImg.urlToImage }}
style ={{height:"100%", width:"100%"}}
/>
)
})}
</Swiper>

发布于 2022-01-26 03:17:38
你可以试试
this.setState({
dataNews:responseJson?.articles, // add articals here
dataBanner:responseJson?.articles // add articals here
})而且您还可以验证
<Swiper>
{this.state.dataBanner?.length > 0 && this.state.dataBanner.map((itemImg) => {
return (
<Image
source={{ uri: itemImg.urlToImage }}
style={{ height: "100%", width: "100%" }}
/>
)
})}
</Swiper>https://stackoverflow.com/questions/70857132
复制相似问题