首页 > 解决方案 > 如何在 React Native 中列出传入的数据

问题描述

我想列出示例中使用的服务中的数据。你能帮助我吗?

在此处输入图像描述

我的代码:

 import React, { Component } from "react";

export default class CustomersTab extends Component {
  constructor(props) {
    super(props);
    this.state = {
      token: "",
      isLoading: true,
      dataSource: null
    };
  }

  componentWillMount() {
    tokenner()
      .then(responseJson => {
        const token = "Bearer " + responseJson.result.token;
        this.setState({ token });
        fetch(
          "apiurl",
          {
            method: "GET",
            headers: {
              Accept: "application/json",
              "Content-Type": "application/json",
              Authorization: token
            }
          }
        )
          .then(response => response.json())
          .then(responseData => {
            this.setState({
              isLoading: false,
              dataSource: responseData.result
            });
          });
      })
      .catch(error => console.warn(error));
  }};
  render() {

        return (
        <View>
          <Text>
            I WANT LİST DATA HERE
          </Text>
        </View> )}}

我试着自己做,但我无法建立循环结构。每次只显示一个数据。我应该怎么办?

标签: reactjsreact-native

解决方案


你必须做这样的事情。将 id 更改为您要显示的内容。

return (
  <View>
    <ListView
    dataSource={this.state.dataSource}
    renderRow={data => <Text>{data.id}</Text>}
    />
  </View>
);

推荐阅读