[英]Can't update dataSource with ListView on React-Native
我正在正確獲取JSON數據,並且可以正常打印,但是我無法將其設置為列表的數據源,因此可以正確更新行。 當打印this.state.dataSource時,它將作為未定義返回。 我返回的JSON是一個數組。
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>
);
}
}
任何幫助,將不勝感激
略微調整后,設法通過此處列出的先前答案之一解決了該問題:
_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))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.