0

I am developing Weather App in React js using axios.I am using axios get method inside handleClick method.Is it ok to use like this.and here I deleted app key but I am using that in my project.

handleClick=()=>
    {
      this.setState({isClicked:true},()=>{
        const url = `https://api.openweathermap.org/data/2.5/weather?q=${this.state.city}&cnt=${this.state.country}&appid=appKey`;
          axios.get(url).then((resp)=>
          {
            this.setState({
              temperature:resp.data.main.temp
            })
          })
        })
    }
Namita
  • 1
  • 1

1 Answers1

0

I think it's okay to use it in click handler. And don't forget to set isClicked as false again, if it was used to prevent double clicking:

handleClick=()=>
    {
      this.setState({isClicked:true},()=>{
        const url = `https://api.openweathermap.org/data/2.5/weather?q=${this.state.city}&cnt=${this.state.country}&appid=appKey`;
          axios.get(url).then((resp)=>
          {
            this.setState({
              temperature:resp.data.main.temp,
            })
          }).finally(() => {
            this.setState({
              isClicked: false
            })
          })
        })
    }

glinda93
  • 7,659
  • 5
  • 40
  • 78