【问题标题】:Error: Invariant violation: tried to get frame for out of range index NaN in react-native-ios and fetch-api错误:不变违规:试图在 react-native-ios 和 fetch-api 中获取超出范围索引 NaN 的帧
【发布时间】:2019-06-30 23:59:56
【问题描述】:

enter image description here尝试使用 fetch-api 在 react-native 中显示博客内容。我对语法感到很困惑,因为它与我上网的相关示例变得越来越复杂。 API 是从 PHP-PDO/MySQL 本地生成的,我在 POSTMAN 上测试了一个 url。但是,每次我尝试使用 fetch-api 和 console.log() 显示内容时都会收到错误消息。

我曾尝试在线检查类似问题,但由于问题与我的不同,这似乎模棱两可。有一次我想是因为我在渲染中使用了 map(),所以我改成了 FlatList。

错误:

This error is located at:
    in VirtualizedList (at FlatList.js:625)
    in FlatList (at Category.js:32)
    in RCTView (at View.js:45)
    in View (at Category.js:31)
    in Category (at App.js:10)
    in RCTView (at View.js:45)
    in View (at App.js:9)
    in App (at renderApplication.js:34)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:33)

import React, { Component } from 'react';
import 
{ Text, 
  View,
  FlatList,
  StyleSheet
} from 'react-native';


export default class Category extends Component {
    constructor(props){
        super(props);
        this.state = {
            data: []
        }
    }
        componentWillMount() {
           const that = this;
            fetchData = async () => {
            const response = await  fetch("http://localhost/rest_api_myblog/api/post/read.php");
            const json = await response.json();
            that.setState({ data: json});
            console.log(json);
          }

          }


          render() {
             return(
               <View style={styles.container}>
                 <FlatList 
                 data={this.state.data}
                 keyExtractor={(x, i) => i}
                 renderItem={({ item }) =>
                 <Text>
                   {`${item.author} ${item.category_name}`}
                 </Text>}
                 />
               </View>
             );

          }
}

我想显示帖子内容

【问题讨论】:

  • 您可以编辑您的问题并粘贴您收到的完整错误消息吗?我还建议您粘贴来自 api 的预期响应
  • 您的错误可能是由于您将对象而不是数组传递给您的 FlatList。将您的状态更改为this.state = { data: [] },您还需要检查您收到的 json 是否是一个数组。
  • 我确实将它从 this.state = { data: [ ]} 更改为 this.state = { data: {} } 因为错误。 console.log() 没有显示任何内容。即使我使用 response.ok。我已经编辑了完全错误的代码
  • 从您的componentWillMount 中删除setState
  • 同样的东西......当我删除它时

标签: ios reactjs react-native fetch-api


【解决方案1】:

我遇到了同样的错误,因为我试图执行对象而不是数组。确保您使用的是 FlatList 数组

【讨论】:

    【解决方案2】:

    我已经使用 axios 调整了您的代码。试试看你是否使用 axios,但你需要使用 npm i axios --save 或 yarn add axios 将 axios 添加到项目中,然后你可以导入它。 所做的将为您提供有关如何处理结果的线索。

    import React, { Component } from 'react';
    import {
        ScrollView, 
        StyleSheet,
        View, 
        Text
    } from 'react-native';
    import axios from 'axios';
    
    export default class Post extends Component{
        constructor(props){
            super(props);
            this.state = {
                posts: []
            }
        }
    
    
        componentDidMount(){
            axios.get(`http://localhost/rest_api_myblog/api/post/read.php`) //I used backtick 
            //.then(json => console.log(json.data.data[0].id)) //try to traverse to your json element by doing console.log to ensure you have a feedback
            .then(json => json.data.data.map(mydata =>(
                {
                    author: `${mydata.author} ${mydata.id}`,
                    id: mydata.registered 
                }
            )))
            //.then(newData => console.log(newData)) //also I did a console.log to check if the newData is working. 
            .then(newData => this.setState({posts: newData}))
            .catch(error => alert(error))
    
            }
        render(){
            return (
            <ScrollView>
                 {   
                     this.state.posts.map((post, index) =>(
                         <Text key={index}>
                             {post.author}
                         </Text>
                     ))
                 }
            </ScrollView>);
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 非常感谢,您的解释确实挽救了这一天。非常明确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    相关资源
    最近更新 更多