【问题标题】:Printing arrays to screen in React Native在 React Native 中将数组打印到屏幕上
【发布时间】:2016-07-25 16:58:25
【问题描述】:

我只想在我的 React Native iOS 应用程序的屏幕上打印几个数组。现在屏幕上没有任何显示。 for 循环后的 console.logs 显示数组具有它们应该具有的数据,但它没有反映在屏幕上。 fetch 调用加载后如何再次渲染?这是我的代码:

'use unique'
import React, { Component } from 'react';
import {
  StyleSheet,
  TouchableHighlight,
  Text,
  View,
  ListView
} from 'react-native';

import api from './apicall';

class APIRequest extends Component {

  constructor() {
    super();

    this.state = {
      users: [],
      id: [],
      type: []
    }
  }

  componentWillMount() {

      api.getData()
        .then((res) => {
          this.setState({
            users: res
          })
          for (var i = 0; i < this.state.users.length; i++) {
            this.state.id.push(this.state.users[i].id);
            this.state.type.push(this.state.users[i].type);
      });
  }

  render() {
    return(
      <View style={styles.container}>
        <Text style={styles.buttonText}>
          {this.state.id}
        </Text>
      </View>
    );
  }
}

【问题讨论】:

    标签: ios arrays react-native


    【解决方案1】:
    this.state.id.push(this.state.users[i].id);
    this.state.type.push(this.state.users[i].type);
    

    这是设置新状态的错误方法。您应该执行以下操作:

    var idArray = [];
    var typeArray = [];
    for (var i = 0; i < this.state.users.length; i++) {
      idArray.push(this.state.users[i].id);
      typeArray.push(this.state.users[i].type);
    }
    this.setState({id: idArray});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2013-05-28
      相关资源
      最近更新 更多