1

I am trying to create React weather app. In this app you can type name of the city and it shows the currently temperature. But after caloing API my state dont want to change to other city object (in coponentDidMount method - "obje" state).

import React, { Component } from 'react';
import Api from './api.js';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      obje: {},
      inputValue: 'Paris'
    }
  }
  componentDidMount() {
    var rootUrl = "http://api.openweathermap.org/data/2.5/weather?q=";
    var city = this.state.inputValue
    var key = "&appid=aa32ecd15ac774a079352bfb8586336a";
      fetch(rootUrl + city + key)
        .then(function(response) {
            return response.json();
        }).then(d => {
          this.setState({obje:d})
        });

  }

  handleChange(event) {
    event.preventDefault();

    this.setState({inputValue: this.refs.inputVal.value});
    console.log(this.refs.inputVal.value);
  }
  render() {
    return (
      <div>
       {this.state.obje.name}
       <form action="" method="" onSubmit={this.handleChange.bind(this)}>
      <input ref="inputVal" type="text" />
      <input type="submit" />
    </form>
      </div>
    );
  }
}

export default App;
2
  • are there any errors in console? Commented Jul 25, 2017 at 17:34
  • no errors occurs Commented Jul 25, 2017 at 17:37

1 Answer 1

1

componentDidMount is only called once - when the component mounts. A state change will not trigger that code again, therefore the XHR request will not be made again. Split out the XHR logic to its own method and call it in both places, for example:

import React, { Component } from 'react';
import Api from './api.js';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      obje: {},
      inputValue: 'Paris'
    }
  }
  getWeather() {
      var rootUrl = "http://api.openweathermap.org/data/2.5/weather?q=";
      var city = this.state.inputValue;
      var key = "&appid=aa32ecd15ac774a079352bfb8586336a";
        fetch(rootUrl + city + key)
          .then(function(response) {
              return response.json();
          }).then(d => {
            this.setState({obje:d})
          });
  }
  componentDidMount() {
     this.getWeather();
  }

  handleChange(event) {
    event.preventDefault();

    this.setState({inputValue: this.refs.inputVal.value}, () => {
        this.getWeather();
    });
    console.log(this.refs.inputVal.value);
  }
  render() {
    return (
      <div>
       {this.state.obje.name}
       <form action="" method="" onSubmit={this.handleChange.bind(this)}>
      <input ref="inputVal" type="text" />
      <input type="submit" />
    </form>
      </div>
    );
  }
}

export default App;
Sign up to request clarification or add additional context in comments.

3 Comments

Your idea dont solve my problem. Its the same effect :(
I didn't test my code, but assuming that your goal is to make an API call when the user clicks submit - this or some variation of this is the solution. Did you check the console and make sure the new value is being logged? Are there errors? Did you try logging from getWeather() to ensure that it is being called correctly?
I think the problem is with variable "city" beacuse function getWeather is triggered only once, so this variable can't change. HM.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.