javascript - React Native - 不变违规 - 试图获取超出范围索引的框架

标签 javascript reactjs database react-native react-native-flatlist

我尝试搜索其他帖子和论坛来解决此错误。然而,要么没有人解决这些问题,要么这些问题并没有真正做与我正在做的事情相同的事情。我收到一条错误消息“不变违规 - 试图获取超出范围索引的框架”。当我尝试从 poke api 将数据输入到平面列表时,就会发生这种情况。请参阅下面的代码。

import React, { useState } from "react";
import { View, Text , Button, FlatList, ActivityIndicator } from "react-native";
import { GlobalStyles } from "../styles/GlobalStyles";

class Home extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource: []
        }
    }

    componentDidMount() {
        fetch("https://pokeapi.co/api/v2/pokemon/")
            .then((res)=> res.json())
            .then((response)=> {
                this.setState({
                    isLoading: false,
                    dataSource: response
                })
                console.log(response)
            })
    }


    render() {
        const showIndicator = this.state.isLoading == true ? <ActivityIndicator size="large" color="#0000ff" /> : null;
        return(
            <View style={GlobalStyles.container}>
                <View style={GlobalStyles.activityIndicator}>{showIndicator}</View>
                <FlatList data={this.state.dataSource} renderItem={({item})=> {
                    return(
                        <View>
                            <Text>{item.name}</Text>
                        </View>
                    )
                }}/>
                <Button onPress={()=> this.props.navigation.navigate("About")} title="Go to about"/>
            </View>
        )
    }
}

export default Home;

最佳答案

您做错的事情是您发送的 this.state.datasource 是您的数据属性,您需要发送 this.state.dataSource.results

import React, { useState } from "react";
import { View, Text , Button, FlatList, ActivityIndicator } from "react-native";

class Home extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource: []
        }
    }

    componentDidMount() {
        fetch("https://pokeapi.co/api/v2/pokemon/")
            .then((res)=> res.json())
            .then((response)=> {
                this.setState({
                    isLoading: false,
                    dataSource: response
                })
                console.log(response)
            })
    }


    render() {
        const showIndicator = this.state.isLoading == true ? <ActivityIndicator size="large" color="#0000ff" /> : null;
        return(
            <View style={{marginTop:100}}>
                <View>{showIndicator}</View>
                <FlatList data={this.state.dataSource.results}
                 renderItem={({item})=> {
                    console.log("item is item",item);
                    return(
                        <View>
                            <Text>{item.name}</Text>
                        </View>
                    )
                }}/>
                <Button onPress={()=> this.props.navigation.navigate("About")} title="Go to about"/>
            </View>
        )
    }
}

export default Home;

enter image description here

希望这有帮助!

关于javascript - React Native - 不变违规 - 试图获取超出范围索引的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61444039/

相关文章:

javascript - 嵌套 react 路由器组件在页面重新加载时不会加载/渲染?

javascript - 悬停时旋转图像失败

database - 带有数据库 : java. lang.ClassNotFoundException : org. springframework.security.core.userdetails.jdbc.JdbcDaoImpl 的 Spring Security 应用程序

reactjs - 接口(interface) 'NodeModule' 无法同时扩展类型 'Module' 和 'Module'

sql - 在 DB2 中更改主键序列( 'Default' 到 'Always')

database - 我如何确定数据源的 Oracle 数据库名称

javascript - 游戏重置按钮无法正常工作。不可点击的 div

javascript - 如果数组的任何元素等于 n 或数组的两个元素之和等于 n,则返回 true - 提高性能

javascript - 开发 JavaScript 以进行缩小

javascript - 结合 react 和 Jade