Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

react props的问题

clipboard.png
子组件的searchKey 是通过props传递过来的。当父组件传递的props.searchKey改变的时候怎么重新render子组件呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

推荐不要在这个组件中处理 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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...