當我更新像陣營本地的ListView清除數據源
if (!this.state.filtered) {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(allData),
filtered: false,
});
} else {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(filteredData),
filtered: true,
});
}
組件的狀態它的做工精細,當filteredData
不空不更新。但是當filteredData
是一個空數組時,ListView不會在setState
(函數renderRow()
和rowHasChanged()
也不會被調用)之後更新。
我試過打電話listView.forceUpdate()
但沒用。 我反應過來,原生版本是0.14.0和運行Android 5.0
===更新===
調用setState()
,使用console.log(this.state.dataSource)
結果之後是
{
_rowHasChanged: [Function],
_getRowData: [Function: defaultGetRowData],
_sectionHeaderHasChanged: [Function],
_getSectionHeaderData: [Function: defaultGetSectionHeaderData],
_dataBlob: { s1: [] },
_dirtyRows: [ [] ],
_dirtySections: [ true ],
_cachedRowCount: 0,
rowIdentities: [ [] ],
sectionIdentities: [ 's1' ]
}
在我的情況下,結構可變allData
是這樣的:
var allData = [[{id:1, name:'tom'},{id:2, name:'jerry'}], [...], [...]];
和filteredData
僅僅是一個空數組var filteredData = [];
我修改filteredData
到var filteredData = [[null]]
和列表視圖可以正確地呈現,不知道爲什麼,它只是工作:(
clonewithRows將始終以輸入作爲數組 所以一定要爲你回答指定數據源本身爲空或空數組到clonewithrows –