[英]Can't update dataSource with ListView on React-Native
I'm fetching my JSON data correctly and it is printing fine, however I cannot set it to the datasource of the list so I can update my rows correctly. 我正在正确获取JSON数据,并且可以正常打印,但是我无法将其设置为列表的数据源,因此可以正确更新行。 When printing the this.state.dataSource it is returning as undefined.
当打印this.state.dataSource时,它将作为未定义返回。 My returned JSON is an array.
我返回的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>
);
}
} }
Any help would be appreciated 任何帮助,将不胜感激
Managed to fix it with one of the previous answers listed here with a slight tweak: 略微调整后,设法通过此处列出的先前答案之一解决了该问题:
_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.