listview - 在 ListView、React-Native 中迭代 renderRow 内的对象数组

标签 listview reactjs react-native axios react-native-listview

有下面的代码, 如何在手机屏幕上显示各种属性? 我可以看到控制台上打印出的对象数组,但无法使用每个对象中包含的属性来在移动屏幕上显示信息。

import React, { Component } from 'react';
import { AppRegistry, ListView, View, Text } from 'react-native';
import axios from 'axios';

export default class Component5 extends Component {
  constructor() {
    super();
    const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
    this.state = {
      userDataSource: ds,
    };
  }
  componentDidMount() {
    this.fetchUsers();
  }
  fetchUsers() {
    axios.get('https://stageapi6.devmn.net/api/v1/forums/threads?topic_id=2418', {
      headers: {
        'client-id': '********'
      }
    })
    .then(response => this.setState({ userDataSource: this.state.userDataSource.cloneWithRows(response) }));
  }

  renderRow = (user) => {
    console.log(user); // => [Object, Object, Object, Object .........]

    return (
      <View>
        {/* I need a way to iterate the bojects and display the properties inside teh Text tags, the code below doesn't work */}
        <Text>{user.thread.num_posts}</Text> 
        <Text>{user.thread.topic_name}</Text>
        <Text>{user.thread.name}</Text>
      </View>
    );
  }
    render() {
      return (
        <ListView
          dataSource={this.state.userDataSource}
          renderRow={(user) => this.renderRow(user.threads)}
        />
      );
    }
}

AppRegistry.registerComponent('Component5', () => Component5);

SCREENSHOT OF THE RETURNED ARRAY OF OBJECTS AND ITS PROPERTIES

最佳答案

您需要迭代 user 数组并动态构建 jsx。使用类似下面的内容

renderRow = (user) => {
 return (
  <View>
    { user.map(u =>
             (<View key={u.thread.id}>
              <Text>{u.thread.num_posts}</Text> 
              <Text>{u.thread.topic_name}</Text>
              <Text>{u.thread.name}</Text>
              <View>));
    }
  </View>
 );
}

关于listview - 在 ListView、React-Native 中迭代 renderRow 内的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43493121/

相关文章:

xaml - 在 ListView MVVM 中绑定(bind)复选框

qt - QML ListView 方法 positionViewAtEnd() 完全相反

java - Android 可滚动布局

javascript - 如何初始化和使用从其他模块(使用 WebPack 和 ES6)导出的 ReactJS 类?

android - 如何使用 React Native 在 Android 上实现 CallKit 相似行为?

listview - flutter 。在非常大的 ListView 上设置 ListView 初始位置

javascript - 图像未在 React 中加载

javascript - 来自相机的 Expo/React Native 图像路径

android - 世博会不断停止安卓模拟器

reactjs - Firebase - 服务器 Firestore 读取服务器端渲染的最佳实践