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 重新刷新

阅读 2k
1 个回答

this.state.customers = resultArray//这段代码谁教你这样写的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题