我现在用了暂时性的写法,就是把rowhaschanged的代码复制到render里,这样可以得到想要的效果,因为父组件setState 并没有触发ListView 的rowhaschanged,render里state数据依然是第一次的空值,现在这种写法应该不是合理写法,正常的我父组件setState 为什么不能触发 子组件ListView的数据更新呢?请有经验的教教我正确写法
class CustomerList extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(this.props.dataSource)
};console.log('constructor');console.log(this.state);
this._renderRow = this._renderRow.bind(this);
}
/*这段不重要,只是嵌套的row
_renderRow(rowData: string, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void){console.log('rowData');
return (
<RowView onSelect={() => {
const data = rowData;
const { navigate } = this.props.navigation;
navigate('CustomerInfo',{ data: data});
highlightRow(sectionID, rowID);
}} data={rowData}/>
)
}
*/
render() {
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(this.props.dataSource)
};
return (
<ListView
enableEmptySections = {true}
dataSource={this.state.dataSource}
renderRow={this._renderRow}
// renderSeparator={this._renderSeparator}
/>
);
}
};