简体   繁体   English

将数组对象渲染到 FlatList React Native

[英]Render Array Objects into FlatList React Native

I' fetching data from axios through my API but when I'm rendering the data of it so it shows me blank template.我通过我的 API 从 axios 获取数据,但是当我渲染它的数据时,它显示了空白模板。 So kindly let me know if I'm doing any mistake.如果我做错了什么,请告诉我。 I pasted my code with api response.我用 api 响应粘贴了我的代码。 Thanks谢谢

console.log => response.data console.log => response.data

Data Array [
  Object {
    "ID": 2466,
    "ItemName": "WWE2K20"
}
]

My Component我的组件

import React, { Component } from 'react';
import { Container, Header, Content, Card, CardItem, Body, Text } from 'native-base';
import { View, StyleSheet, FlatList, Image } from 'react-native';
import axios from 'axios';

export default class HomeScreen extends Component {
    constructor() {
        super()
        this.state = {
            itemList: []
        }
    }
    async componentDidMount() {
        await axios.get('myapiuri')
            .then((response) => {
                this.setState({
                    itemList: response.data
                });
                console.log("Data", this.state.itemList)
            })
            .catch(error=>console.log(error))
    }

    render() {
        return (
            <Container>
                <Content>
                    <FlatList
                        data={this.state.itemList}
                        renderItem={(item) => {
                            return(
                                <Text>{item.ItemName}</Text>
                            );
                        }}
                        keyExtractor={(item) => item.ID}
                    />
                </Content>
            </Container>
        );
    }
}

There are a few problems I see.我看到了一些问题。

First you need to use ({item}) in renderItem as stated in comments.首先,您需要在 renderItem 中使用 ({item}),如评论中所述。

Second, you are mixing async/await with then block.其次,您将 async/await 与 then 块混合在一起。 In this case there is no need to async/await.在这种情况下,不需要异步/等待。

So your code must be:所以你的代码必须是:

export default class HomeScreen extends Component {
  constructor() {
    super();
    this.state = {
      itemList: []
    };
  }

  componentDidMount() {
    axios
      .get("myapiuri")
      .then(response => {
        this.setState({
          itemList: response.data
        });
        console.log("Data", this.state.itemList);
      })
      .catch(error => console.log(error));
  }

  render() {
    return (
      <Container>
        <Content>
          <FlatList
            data={this.state.itemList}
            renderItem={({ item }) => {
              return <Text>{item.ItemName}</Text>;
            }}
            keyExtractor={item => item.ID}
          />
        </Content>
      </Container>
    );
  }
}

If you still having problems, please look at this example:如果您仍然遇到问题,请查看以下示例:

https://snack.expo.io/@suleymansah/c0e4e5 https://snack.expo.io/@suleymansah/c0e4e5

                    <FlatList
                        data={this.state.itemList}
                        renderItem={**(item)** => {
                            return(
                                <Text>{item.ItemName}</Text>
                            );
                        }}
                        keyExtractor={(item) => item.ID}
                      />

in here "item" is a object, it has, ID and ItemName.这里的“item”是一个 object,它有 ID 和 ItemName。 when you call like this item is not recognize as a object that is why nothing show,当你像这个项目一样打电话时,它不会被识别为 object 这就是为什么没有显示,

({ item }) ({ 物品 })

you should change above call as this.你应该像这样改变上面的调用。 then item recognize as a object and you can call its attributes as, item.Id or item.ItemName然后项目识别为 object,您可以将其属性称为 item.Id 或 item.ItemName

i think you got my point why your code is not working.我想你明白了为什么你的代码不起作用。 please learn react-native life cycle.请了解 react-native 生命周期。 componentDidMount didnt want async, after component render componentDidMount will call and componentWillMount call at one time when component loading.componentWillReceiveProps call when any state of the component change. componentDidMount 不需要异步,在组件渲染后 componentDidMount 会在组件加载时调用,并且 componentWillMount 会在组件加载时同时调用。当组件的任何 state 发生变化时,componentWillReceiveProps 会调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM