简体   繁体   English

在反应中调用外部 componentDidMount()

[英]calling externally componentDidMount() in react

I have a requirement in which once page gets loaded my dropdownlist should be populated.我有一个要求,一旦页面被加载,我的dropdownlist应该被填充。 for that I put that code in componentDidMount() .为此,我将该代码放在componentDidMount()中。

  componentDidMount() {
    axios.get(`http://localhost:8080/country_code`).then((res) => {
      const countryData = res.data;
      this.setState({ countryData });
      alert(countryData);
    });
  }

I have one user input field in which person enter the value and save it into database.我有一个用户输入字段,人们在其中输入值并将其保存到数据库中。 I want once user save that value into DB, my dropdown should get refresh and that value should be visible in the dropdown.我希望一旦用户将该值保存到数据库中,我的下拉列表应该会刷新,并且该值应该在下拉列表中可见。 How can I externally call componentDidMount() ?如何从外部调用componentDidMount() is there any better way to handle the same?有没有更好的方法来处理同样的事情?

As of now list is getting refreshed only when user resfresh the page.截至目前,列表仅在用户重新刷新页面时才会刷新。

You can't call componentDidMount externally but you can extract the code in componentDidMount to a method and can call it in both componentDidMount and onSave .您不能在外部调用componentDidMount ,但可以将componentDidMount中的代码提取到方法中,并且可以在componentDidMountonSave中调用它。

 alertDropDown = () => {
    axios.get(`http://localhost:8080/country_code`).then((res) => {
          const countryData = res.data;
          this.setState({ countryData });
          alert(countryData);
        });
    }

componentDidMount组件DidMount

componentDidMount() {
  this.alertDropDown()
}

On DB save method关于数据库保存方法

onSave = () => {
 this.alertDropDown()
}

You can't call externally componentDidMount() method.. so you need set common function which is call in componentDidMount() and onChange dropdown value.您不能从外部调用componentDidMount()方法。所以您需要设置在componentDidMount()和 onChange 下拉值中调用的通用 function。 see below code !见下面的代码!

class App extends Component {
  componentDidMount() {
    this.handleCallApi();
  }

  handleCallApi = () => {
    axios.get(`http://localhost:8080/country_code`).then((res) => {
      const countryData = res.data;
      this.setState({ countryData });
      alert(countryData);
    });
  }

  render() {
    return (
      <div>
        <button onClick={this.handleCallApi}>Call Api</button>
      </div>
    );
  }
}
export default App;

You can't call the componentDidMount() , as it's a lifecycle method and is called at initial render.您不能调用componentDidMount() ,因为它是一个生命周期方法并且在初始渲染时被调用。 You can expose a function and call that function from inside the componentDidMount() something like:您可以公开 function 并从componentDidMount()内部调用 function ,例如:

updateDropdownData = () => {
 axios.get(`http://localhost:8080/country_code`).then((res) => {
      const countryData = res.data;
      this.setState({ countryData });
      alert(countryData);
    });
}

componentDidMount() {
  this.updateDropdownData()
}

And you can call this.updateDropdownData() from anywhere you want.你可以从任何你想要的地方调用this.updateDropdownData() Just like:就像:

onSomeUserAction = () => {
 this.updateDropdownData()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM