React.js, किसी फ़ंक्शन को ट्रिगर करने से पहले सेटस्टैट के समाप्त होने तक प्रतीक्षा करें?


107

यहाँ मेरी स्थिति है:

  • on.handleFormSubmit () मैं इसे निष्पादित कर रहा हूं। ssetState ()
  • this.handleFormSubmit () के अंदर, मैं इसे कॉल कर रहा हूं .findRoutes (); - जो इस के सफल समापन पर निर्भर करता है। शुरू ()
  • this.setState (); इससे पहले पूरा नहीं होता है ।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>
                );
            }
        });

जवाबों:


246

setState()एक वैकल्पिक कॉलबैक पैरामीटर है जिसे आप इसके लिए उपयोग कर सकते हैं। आपको केवल अपना कोड थोड़ा बदलना होगा:

// Form Input
this.setState(
  {
    originId: input.originId,
    destinationId: input.destinationId,
    radius: input.radius,
    search: input.search
  },
  this.findRoutes         // here is where you put the callback
);

सूचना कॉल करने के लिए findRoutesअंदर अब है setState(), कॉल दूसरा पैरामीटर के रूप में।
बिना ()इसलिए कि आप फंक्शन पास कर रहे हैं।


2
गजब का! बहुत बहुत धन्यवाद
पुरुष

यह ReactNative में setState के बाद एक AnimatedValue को रीसेट करने के लिए अच्छी तरह से काम करेगा।
SacWebDeveloper 23

ग्रेट ~ बहुत बहुत धन्यवाद
強 a

2
एक सामान्य संस्करणthis.setState({ name: "myname" }, function() { console.log("setState completed", this.state) })
ससी वरुण

ऐसा लगता नहीं है कि आप एक से अधिक कॉलबैक को सेटस्टेट कर सकते हैं। क्या कॉलबैक का पीछा करने का एक गैर-गड़बड़ तरीका है? आइए बताते हैं कि मुझे 3 तरीके मिले हैं जिन्हें सभी को चलाने की आवश्यकता है, और सभी अद्यतन स्थिति। इसे संभालने का पसंदीदा तरीका क्या है?
शॉन

17
       this.setState(
        {
            originId: input.originId,
            destinationId: input.destinationId,
            radius: input.radius,
            search: input.search
        },
        function() { console.log("setState completed", this.state) }
       )

यह मददगार हो सकता है


10

setState()नए राज्य के डॉक्स के अनुसार कॉलबैक फ़ंक्शन में परिलक्षित नहीं हो सकता है findRoutes()। यहाँ रिएक्ट डॉक्स से अर्क है :

setState () तुरंत इसे बदल नहीं पाता है। यह स्थिर है, लेकिन एक लंबित राज्य संक्रमण बनाता है। इस पद्धति पर कॉल करने के बाद एक्सेस करना संभव है।

कॉल सेट करने के लिए सिंक्रोनस ऑपरेशन की कोई गारंटी नहीं है और प्रदर्शन के लाभ के लिए कॉल को बैच किया जा सकता है।

तो यहाँ है कि मैं तुम्हें क्या करना चाहिए का प्रस्ताव है। आपको inputकॉलबैक फ़ंक्शन में नए राज्यों को पास करना चाहिए findRoutes()

handleFormSubmit: function(input){
    // Form Input
    this.setState({
        originId: input.originId,
        destinationId: input.destinationId,
        radius: input.radius,
        search: input.search
    });
    this.findRoutes(input);    // Pass the input here
}

findRoutes()समारोह इस तरह परिभाषित किया जाना चाहिए:

findRoutes: function(me = this.state) {    // This will accept the input if passed otherwise use this.state
    if (!me.originId || !me.destinationId) {
        alert("findRoutes!");
        return;
    }
    var p1 = new Promise(function(resolve, reject) {
        directionsService.route({
            origin: {'placeId': me.originId},
            destination: {'placeId': me.destinationId},
            travelMode: me.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
}

इसका एक गंभीर दोष है - setState()नए राज्य के रूप में शाब्दिक ऑबजिट पास करना अच्छा नहीं है क्योंकि यह दौड़ की स्थिति की ओर जाता है
टार

यहाँ प्रतिक्रिया डॉक से एक और उद्धरण (जो आपके जवाब पोस्ट किए जाने के बाद से अपडेट किया गया हो सकता है): "... घटकडीड यूपडेट या एक सेटस्टैट कॉलबैक (सेटस्टेट (अपडेटर, कॉलबैक)) का उपयोग करें, जिसमें से किसी एक को अपडेट के बाद आग लगने की गारंटी दी जाती है। लागू किया गया है ”। यह मेरे लिए कहता है कि नया राज्य निश्चित रूप से कॉलबैक फ़ंक्शन में परिलक्षित होता है।
एंडी
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.