首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义不是函数(靠近‘...this.state.dataBanner.map.’)

未定义不是函数(靠近‘...this.state.dataBanner.map.’)
EN

Stack Overflow用户
提问于 2022-01-25 23:47:17
回答 1查看 39关注 0票数 0

我已经用同样的问题检查了一些类似的问题:ReactJS TypeError: undefined is not a function (near '...this.state.data.map...')

我遵循了每一个步骤:dataBanner:[]dataBanner:responseJson

代码语言:javascript
复制
...
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)
    })
}

但是下面的代码仍然给出了一个错误:

代码语言:javascript
复制
            <Swiper>
                {this.state.dataBanner.map((itemImg)=>{
                    return (
                        <Image
                            source ={{ uri: itemImg.urlToImage }}
                            style ={{height:"100%", width:"100%"}}
                        />
                    )
                })}
            </Swiper>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-26 03:17:38

你可以试试

代码语言:javascript
复制
        this.setState({
            dataNews:responseJson?.articles, // add articals here
            dataBanner:responseJson?.articles // add articals here
        })

而且您还可以验证

代码语言:javascript
复制
<Swiper>
    {this.state.dataBanner?.length > 0 && this.state.dataBanner.map((itemImg) => {
        return (
            <Image
                source={{ uri: itemImg.urlToImage }}
                style={{ height: "100%", width: "100%" }}
            />
        )
    })}
</Swiper>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70857132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档