React native 更新this.state.dataSource不会';更新列表视图
我有一个列表视图,如:React native 更新this.state.dataSource不会';更新列表视图,react-native,React Native,我有一个列表视图,如: <ListView dataSource={this.state.dataSource} renderRow={this.renderMovie} style={styles.listView} /> 它显示的行如下所示: <View style={styles.container}> <TouchableHighlight onPress={this._onPressButton.b
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderMovie}
style={styles.listView}
/>
它显示的行如下所示:
<View style={styles.container}>
<TouchableHighlight onPress={this._onPressButton.bind(this,movie)}>
<Image
source={{uri: movie.uri}}
style={styles.thumbnail}
/>
</TouchableHighlight>
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.id}</Text>
<Text style={styles.year}>{movie.votes}</Text>
</View>
</View>
{movie.id}
{电影.投票}
我有一个函数可以用电影的“投票数”更新this.state.dataSource。但是这些更改没有反映在UI中,但是当我记录this.state.dataSource时,更改就在那里。我是否需要以某种方式重新渲染行?他们不应该自动改变吗
谢谢 我也有类似的情况,我唯一要做的就是:
this.setState({
dataSource: this.ds.cloneWithRows(new_data_for_datasource)
})
这对我来说很好。
在类构造函数中,我有以下代码:
this.ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
})
this.state = {
dataSource: this.ds.cloneWithRows(this.props.original_data)
}
希望有帮助 要使React native重新渲染行,需要创建数组的副本,更新对象,然后对新创建的数组使用setState。例如:
let newArray = this.state.dataSource.slice();
newArray[indexToUpdate] = {
...this.state.dataSource[indexToUpdate],
field: newValue,
};
this.setState({
dataSource: this.state.dataSource.cloneWithRows(newArray),
});
参考:
fetchCarData(datax) {
fetch(REQUEST_URL + '&' + 'place=' + datax)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
loaded: true,
});
if (responseData.notfound === 'yes') {
alert('No Vehicles found');
this.setState({
notfound: 'Not found vehicles, do another search'
})
} else {
this.setState({
notfound: ''
})
}
})
.done();
}
我看到的问题是,您有一个“新的数据源”,但我的新数据源将是当前的数据源。如中所示,我更改了this.state.dataSource。所以我不能做dataSource:this.ds.cloneWithRows(this.state.dataSource)对吗?与中一样,我没有新的数据源对象。我想我可以将一个设置为当前状态数据源的副本?我不明白在不更新数据源的情况下如何重新加载ListView。您能告诉我您在哪里创建初始数据源的代码吗?在这个数据源中,您必须使用cloneWithRows传递一些数据,或者数据源来自以前的组件,通过props传递?
fetchData:function(){fetch(REQUEST_URL)。然后((response)=>response.json())。然后((responseData)=>{this.setState({dataSource:this.state.dataSource.cloneWithRows)(responseData.gifs),loaded:true,});}).done();},
^这就是我最初获取数据的方式