我已经创建了ListView
这样的内容:
var data = [{id: 1, count: 1}, {id: 2, count: 1}];
var ds = new ListView.DataSource({
rowHasChanged: function (row1, row2) {
console.log('changed');
return true;
}
});
var ListViewExample = React.createClass({
getInitialState: function () {
return {
dataSource: ds.cloneWithRows(data)
}
},
componentDidMount: function() {
setTimeout(()=>{
console.log('changing...');
data[0].count += 1;
this.setState({
dataSource: ds.cloneWithRows(data)
});
},3000);
},
render: function() {
console.log('render')
return (
<ListView
initialListSize={3}
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData.id}:{rowData.count}</Text>}
/>
);
}
});
运行此代码并显示日志:
[tid:com.facebook.React.JavaScript] 'Running application "mytest" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF'
2015-08-03 20:40:10.385 [info][tid:com.facebook.React.JavaScript] 'render'
2015-08-03 20:40:13.396 [info][tid:com.facebook.React.JavaScript] 'changing...'
2015-08-03 20:40:13.398 [info][tid:com.facebook.React.JavaScript] 'render'
当数据发生变化时,视图也发生了变化。
但为什么rowHasChanged
没有被触发? (永远不会看到日志changed
)
答案 0 :(得分:1)
也许你应该看看这个:
https://stackoverflow.com/a/33871118/813675
简而言之,改变
this.setState({
dataSource: ds.cloneWithRows(data)
});
到
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data)
});
希望得到帮助。