Ami*_*aie 2 components render reactjs lifecycle-hook
我有一个组件,它从两个或三个 API 接收新闻列表。组件第一次渲染时,将调用 api 并以componentDidMount 
如下方式渲染数据:
componentDidMount() {
        this.state.platforms.forEach((platform, i) => {
            let objToSend = {
                phrase: this.props.searchParams.phrase,
                // this is the main place when input data changes
                ...this.props.searchParams.general_params,
                ...this.props.searchParams.platforms[i].api_params,
                stringPath: platform,
                apiPath: this.props.searchParams.platforms[i].apiPath,
            }
            this.props.loadData(objToSend)
            // this is the api call using redux which sends data as props to this component
 }
new 当短语更改时,我希望此组件重新渲染并重新运行 componentDidMount,但它不起作用,因为 componentDidMount 将运行一次。所以我使用了componentDidUpdate,但是由于有很多调用,所以api正在不断更新。
每次更改短语时,如何使组件重新渲染并重新运行 componentDidMount
您可以使用componentDidUpdate参数 ( previousProps, previousState) 来检查是否发生了一些新的更改。
例子
componentDidUpdate(previousProps, previousState) {
    if (previousProps.phrase !== this.props.phrase) {
        //Do whatever needs to happen!
    }
}
我以这种方式停止了我的情况的无限循环。
| 归档时间: | 
 | 
| 查看次数: | 9002 次 | 
| 最近记录: |