यहाँ मेरी स्थिति है:React.js, फ़ंक्शन ट्रिगर करने से पहले सेटस्टेट को समाप्त करने के लिए प्रतीक्षा करें?
-
this.handleFormSubmit पर
- () मैं this.setState() this.handleFormSubmit(), मैं this.findRoutes बोल रहा हूँ अंदर
- () को क्रियान्वित कर रहा हूँ; - जो इस .setState()
- this.setState() के सफल समापन पर निर्भर करता है; इससे पहले पूरा नहीं होता है। findRoutes को बुलाया जाता है ...
- मैं इस के अंदर इस .setState() के अंदर कैसे प्रतीक्षा करूं ..handleFormSubmit() इसे कॉल करने से पहले खत्म करने के लिए .findRoutes()?
एक subpar समाधान:
- this.findRoutes() डाल componentDidUpdate में()
- इस क्योंकि वहाँ अधिक राज्य परिवर्तन असंबंधित हो जाएगा findRoutes() फ़ंक्शन स्वीकार्य नहीं है। जब असंबंधित स्थिति अद्यतन होती है तो मैं findRoutes() फ़ंक्शन को ट्रिगर नहीं करना चाहता हूं।
मैं प्रतिक्रिया करने के लिए नया हूं ... तो कृपया मेरे साथ बेकार! नीचे
कोड कृपया देखें झलकी नीचे:
handleFormSubmit: function(input){
// Form Input
this.setState({
originId: input.originId,
destinationId: input.destinationId,
radius: input.radius,
search: input.search
})
this.findRoutes();
},
handleMapRender: function(map){
// Intialized Google Map
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();
this.setState({map: map});
placesService = new google.maps.places.PlacesService(map);
directionsDisplay.setMap(map);
},
findRoutes: function(){
var me = this;
if (!this.state.originId || !this.state.destinationId) {
alert("findRoutes!");
return;
}
var p1 = new Promise(function(resolve, reject) {
directionsService.route({
origin: {'placeId': me.state.originId},
destination: {'placeId': me.state.destinationId},
travelMode: me.state.travelMode
}, function(response, status){
if (status === google.maps.DirectionsStatus.OK) {
// me.response = response;
directionsDisplay.setDirections(response);
resolve(response);
} else {
window.alert('Directions config failed due to ' + status);
}
});
});
return p1
},
render: function() {
return (
<div className="MapControl">
<h1>Search</h1>
<MapForm
onFormSubmit={this.handleFormSubmit}
map={this.state.map}/>
<GMap
setMapState={this.handleMapRender}
originId= {this.state.originId}
destinationId= {this.state.destinationId}
radius= {this.state.radius}
search= {this.state.search}/>
</div>
);
}
});
कमाल! बहुत बहुत धन्यवाद – matthewalexander
आपने मुझे बचाया, धन्यवाद –
यह रीएक्टनेटिव में सेटस्टेट के बाद एनिमेटेड वैल्यू को रीसेट करने के लिए अच्छी तरह से काम करेगा। – SacWebDeveloper