사용자가 위치를 입력하는 텍스트 영역이 있습니다. 그런 다음 onBlur에서 날씨의 값이 설정되고 사용자가 입력 한 값에 따라 결과가 변경됩니다. 코드가 정상적으로 작동합니다. 그러나 사용자가 두 번 발사 할 때만 가능합니다.ReactJS를 사용하여 텍스트 영역을 기반으로 즉시 값을 변경 하시겠습니까?
var Duck = React.createClass({
getInitialState:function() {
return {
deesfault: 'Houston, Texas',
setting: true,
main: '',
}
},
componentDidMount:function() {
return this.callthebase();
},
callthebase: function() {
var selfish = this;
$.get('http://api.openweathermap.org/data/2.5/weather?q='+this.state.deesfault+"&units=imperial&appid=~~appid~~", function(data){
selfish.setState(data);
});
},
changeit: function() {
this.setState({setting: false});
},
setsetting: function() {
this.callthebase();
this.setState({deesfault: document.getElementById('section').value});
this.setState({setting: true});
},
changesection: function() {
if (this.state.setting) {
return (<p onClick={this.changeit}>In {this.state.deesfault}</p>)}
else { return (<textarea id="section" defaultValue={this.state.deesfault} onBlur={this.setsetting} />) }
},
render: function() {
return (
<div>
{this.changesection()}
<p>The weather is {this.state.main.temp}</p>
</div>
)
}
});
은 그대로, 나는 위치를 입력 그것의 흐리게 한 다음 활성화에 돌아와 다음 실제로 내가하고 싶은 일을하기 위해 코드를 다시 흐리게 할 것이다. 이것은 일반 더미 텍스트를 사용할 때 문제가되지 않는 것으로 보입니다. 그러나 API 호출을 만들 때이 문제가 발생합니다.
작동합니다 arrow functions from ES6을 사용할 수 있습니다 'this.setState ({deesfault : newValue, setting : true}); ' –
@GaleelBhasha yup, edited! –