【问题标题】:React-Native TypeError: undefined is not a function (near '...this.state.feeds.map...')React-Native TypeError: undefined is not a function (near '...this.state.feeds.map...')
【发布时间】:2019-09-12 22:11:20
【问题描述】:

我正在尝试在我的应用屏幕上显示来自 url 的新闻提要。我已经为它(解析器)制作了一个组件,并将它导入到我的 master.js 文件中。

我正在使用 axios 从网址获取数据。

我在学习教程时编写了这段代码,这段代码在视频中有效,但不适用于我。

编译时,应用显示错误TypeError: undefined is not a function (near '...this.state.feeds.map...')

我在跑步
世博会:3.0.1
react-native-cli: 2.0.1
反应原生:0.57.1​​
npm:6.11.3

获取数据的组件代码为:

constructor(){
    super();
    this.state = {
        feeds: []
    }
}

componentWillMount(){
    axios.get('https://cointelegraph.com/rss')
      .then(response => this.setState({ feeds: response.data }));
}

renderFeeds = () => {
    return this.state.feeds.map(feed => <Text>{feed.title}</Text>);
}

render(){
    return (
        <View>
            {this.renderFeeds()}
        </View>
    );
}

显示的错误是: Error Image

TIA

【问题讨论】:

  • 你确定response.data 是一个数组吗?它可能是一个会引发此错误的字符串。
  • 我建议您使用 componentDidMount 进行 API 调用。
  • 来自https://cointelegraph.com/rss 的响应采用XML 格式。

标签: react-native


【解决方案1】:

map 函数只存在于数组中。 确保 response.data 是一个数组。

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2020-02-15
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多