react.js - ListView作为子组件,父组件的state作为datasource,如何随着父组件state改变而刷新
本文介绍了react.js - ListView作为子组件,父组件的state作为datasource,如何随着父组件state改变而刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
class 父组件 extends Component {
constructor(props) {
super(props);console.log('module');
this.state= {text: '',customers : []};
this.searchCustomer = this.searchCustomer.bind(this);
}
searchCustomer(text){console.log(text);
this.state.text = text.text;
let t = text.text;
const bagArray = this.props.data;
const resultArray = [];
if(t.length > 0 && bagArray){
for(let i=0;i<bagArray.length;i++) {
if(bagArray[i].name.indexOf(t) >= 0 || bagArray[i].iphone.indexOf(t)>=0 || bagArray[i].wxNumber.indexOf(t)>=0){
resultArray.push(bagArray[i]);
}
}
}
this.state.customers = resultArray;console.log('state');console.log(this.state);
}
render() {
return (
<View style={styles.container}>
<SearchInput callback={this.searchCustomer}/>
<CustomerList dataSource={this.state.customers}/>//这是子组件
</View>
);
}
}
这段就是父组件的代码,第一次渲染是空的Array,等输入搜索词之后通过searchCustomer方法的回调,给父组件的 state.customer赋值,子组件customerList 是一个 ListView ,父组件的state 的改变后 怎么能做到子组件customerlist 重新刷新
解决方案
this.state.customers = resultArray//这段代码谁教你这样写的
这篇关于react.js - ListView作为子组件,父组件的state作为datasource,如何随着父组件state改变而刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文