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;