2016-07-25 6 views
0

단순히 React Native iOS 앱의 화면에 몇 개의 배열을 인쇄하고 싶습니다. 지금은 아무것도 화면에 나타나지 않습니다. for 루프 다음의 console.logs는 배열에 있어야하는 데이터가 있음을 보여 주지만 화면에는 반영되지 않습니다. 가져 오기 호출이로드 된 후 다시 렌더링하려면 어떻게해야합니까? 여기에 내 코드입니다 :React Native 화면 배열 인쇄

'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> 
    ); 
    } 
} 
새로운 상태를 설정하는 잘못된 방법

답변

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}); 
관련 문제