【发布时间】:2016-02-26 17:48:59
【问题描述】:
我正在制作一个简单的 React Native 应用程序,它有多个列表,它让用户有机会更新任何列表中的项目或向列表中添加新项目。
但是,正如之前在 StackOverflow 中指出的那样,不能将项目添加或推送到 ListView,因为它应该是不可变的数据结构。即在此代码中,无法将新项目推送到数据源中
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
var data = ds.cloneWithRows(responseData)
var obj = {};
obj['someproperty'] = 'foo';
data.push(obj); //cannot push a new item into data
所以我所做的是保留我从服务器获取的数据的副本(下面的代码中的this.state.items;)(我称之为cloneWithRows),附加到该副本(或更新它)如果是编辑),然后在发生更改时(通过编辑或创建新项目)随时在变异副本上调用 cloneWithRows 并更新 ui,然后更新服务器(最后一部分超出范围的反应本机应用程序).. 总而言之,我有一个数据副本,当我进行更改时,我可以对其进行变异,用作ds.cloneWithRows 的数据源
但是,保留一个数据结构的缓存似乎没有多大意义,我可以改变它的唯一目的是使用它作为不可变数据结构的源(下面的代码中的listForUI: ds.cloneWithRows(t))如果我想乐观地更新 UI(即在保存到服务器之前显示更改)。
替代方案(对我来说也没有多大意义)似乎是更新服务器,并在我更新或向列表中添加新项目时等待响应(不保留任何类型的缓存),但这会很慢?
问题:没有保留数据的可变副本作为缓存,有没有办法在 react native 中乐观地更新 ui?
addNewItemToList: function(){
var t = this.state.items; //my cache of list items
var newListItem = {};
newListItem['id'] = 123;
t.push(newListItem); //adding-mutating the cache
//creating a new immutable ds
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
listForUI: ds.cloneWithRows(t)
}, function(){
//update server with change
fetch('http://localhost:8080/accounts/1/lists/3/todos', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
//code ommitted
}
}
【问题讨论】:
标签: reactjs react-native