react.js - react props的问题
本文介绍了react.js - react props的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
子组件的searchKey 是通过props传递过来的。当父组件传递的props.searchKey改变的时候怎么重新render子组件呢
解决方案
推荐不要在这个组件中处理 search 方法,获取数据, 应该在父组件(存储 searchKey 所在的组件).
父组件处理完成后, 通过 setState
更新状态, 进而更新 JobList 组件.
如果一定要在这个组件里处理获取数据也是可以, 如下
componentWillReceiveProps (nextProps) {
const nextSearchKey = nextProps.searchKey;
const curSearchKey = this.props.searchKey;
if (nextSearchKey !== curSearchKey) {
// 在这里处理 ajax 请求
const results = ajaxMock(); // 模拟
// 获取到结果后更新 state
this.setState({
lists: results
})
}
}
我建议,看下容器组件跟展示组件分离,相关文章https://www.qcloud.com/commun...
这篇关于react.js - react props的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文