React.js,在触发函数之前等待setState完成? [英] React.js, wait for setState to finish before triggering a function?

查看:430
本文介绍了React.js,在触发函数之前等待setState完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

  • 在this.handleFormSubmit()上,我正在执行this.setState()
  • 在this.handleFormSubmit()内部,我叫this.findRoutes(); -取决于this.setState()的成功完成
  • this.setState();在this.findRoutes之前无法完成...
  • 在调用this.findRoutes()之前,如何等待this.handleFormSubmit()内部的this.setState()完成?
  • on this.handleFormSubmit() I am executing this.setState()
  • inside this.handleFormSubmit(), I am calling this.findRoutes(); - which depends on the successful completion of this.setState()
  • this.setState(); does not complete before this.findRoutes is called...
  • How do I wait for this.setState() inside of this.handleFormSubmit() to finish before calling this.findRoutes()?

不合格的解决方案:

  • 将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>
                );
            }
        });

推荐答案

setState()具有可用于此目的的可选回调参数.您只需要对此稍作更改即可.

setState() has an optional callback parameter that you can use for this. You only need to change your code slightly, to this:

// 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()调用中, 作为第二个参数.
不使用(),因为您正在传递函数.

Notice the call to findRoutes is now inside the setState() call, as the second parameter.
Without () because you are passing the function.

这篇关于React.js,在触发函数之前等待setState完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆