我有一个组件,它接收来自两个或三个 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