无法在 React-Native 上使用 ListView 更新数据源
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))
}
我正在正确获取我的 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))
}