Javascript 如何在使用已更新的从属值调度操作后更新api调用

Javascript 如何在使用已更新的从属值调度操作后更新api调用,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我有以下App.js: constructor(props) { super(props); this.props.setLoading(); } componentDidMount(){ this.convert(); } changeFromCurr = (event) => { this.props.setFromCurrency(event.target.value); this.convert(); } c

我有以下App.js:

  constructor(props) {
    super(props);
    this.props.setLoading();
  }

  componentDidMount(){
    this.convert();
  }

  changeFromCurr = (event) => {
    this.props.setFromCurrency(event.target.value);
    this.convert();
  }

  changeToCurr = (event) => {
    this.props.setToCurrency(event.target.value);
    this.convert();
  }

  changeAmount = (event) => {
    this.props.setValue(event.target.value);
  }

  convert = () => {
    return this.props.convertCurr(this.props.fromCurrency,this.props.toCurrency,this.props.value);
  }

  render() {
    const {fromCurrency, toCurrency, value, result} = this.props;
    return (
      <div>
        <CurrencyForm
          fromCurrency={fromCurrency}
          toCurrency={toCurrency}
          amount={value}
          changeFromCurr={this.changeFromCurr}
          changeToCurr={this.changeToCurr}
          changeAmount={this.changeAmount}
          result={result}
        />
它调用此服务:

import axios from 'axios';


let convertCurrency = async (from,to,amt) => {
    const API_KEY= `b688884ff57c3e17139e632b5f852755`;
    const convertUrl = `http://data.fixer.io/api/latest?
    access_key=${API_KEY}&base=${from}&symbols=${to}`
  try {
    const response = await axios.get(convertUrl);
    console.log(response);
    return response;
  }
  catch (err) {
    console.log('fetch failed', err);
  }
}

export default convertCurrency;
现在,我要做的是,在我的Redux应用商店更新了newfromCurrency属性后,在以下情况下启动此服务调用:

this.props.setFromCurrency(event.target.value);
更新状态,我希望使用新值调用服务。 任何帮助都将不胜感激。

使用react生命周期


这是最好的处理方法吗?我从React componentDidUpdate的文档中了解到,很少使用任何替代方法来实现此目的?请随意使用该链接中的三个生命周期中的任何一个,并使用另一个作为报废生命周期是不可取的。你唯一需要关心的是,你的道具、统计数据、组件的设计模式是否有更好/更清晰的方法来减少使用生命周期内的复杂逻辑。谢谢@keikai。我有一个单点的数据存储,没有本地状态。另外,我的服务调用不操纵请求参数,因此没有无限循环的风险。干杯
this.props.setFromCurrency(event.target.value);
componentDidUpdate(prevProps, prevState) {
  if (yourMethodToCheckIfNotEqual(prevProps.fromCurrency, this.props.fromCurrency)) {
    // Fire your service call
    // Notice that if your service call changes fromCurrency above may cause infinite loop
  }
}