2017-07-29 74 views
0

我正确读取我的JSON数据并且打印正常,但是我无法将其设置为列表的数据源,因此我可以正确更新我的行。当打印this.state.dataSource时,它将以未定义的方式返回。我返回的JSON是一个数组。无法使用React-Native上的ListView更新数据源

export default class CoinCheckerRN extends React.Component { 

constructor(props) { 
    super(props); 
    const dataSource = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}); 
    this.state = { 
     dataSource: dataSource.cloneWithRows(['row 1', 'row 2']), 
    }; 

    this._renderRow = this._renderRow.bind(this); 

} 

componentDidMount() { 
    getCryptocurrencyData().then(function(result) { 
     console.log('??????', result[0].name) 
     this.setState({ dataSource: this.state.dataSource.cloneWithRows(result)}); 
     console.log('??', this.state.dataSource); 

    }).catch(function(error) { 
     console.log('!!!!!', error) 
    }); 



} 

_renderRow(data) { 
    return (
     <CoinCell coinName={'Bitcoin'} coinPrice={'£1,000'} coinPercentageChange={'-4.2%'}></CoinCell>  ) 
} 

render() { 
    return (
     <View> 
      <ListView 
       enableEmptySections 
       ref={'resultListView'} 
       dataSource={this.state.dataSource} 
       renderRow={(data) => this._renderRow(data)} 
       renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator} />} 
       renderHeader={() => <Header />} 
      /> 
     </View> 
    ); 
} 

}

任何帮助,将不胜感激

+0

你看到任何东西在ListView中? –

回答

0

管理与有轻微的调整此处所列的前一个答案的一个修正:

_getCoinData() { 
    getCryptocurrencyData().then(function(result) { 

     const ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}); 
     this.setState({ 
      dataSource: ds.cloneWithRows(result), 
      jsonData: result 
     }); 

     console.log('!!!', this.state.jsonData); 
    }.bind(this)) 
}