sách gpt4 ăn đã đi

javascript - 在 React 中触发函数之前如何等待 setState 完成?

In lại 作者:IT王子 更新时间:2023-10-29 02:48:23 25 4
mua khóa gpt4 giày nike

这是我的情况:

  • 我在 this.handleFormSubmit() 上执行 this.setState()
  • 在 this.handleFormSubmit() 中,我调用 this.findRoutes(); - 这取决于 this.setState() 的成功完成
  • this.setState();在调用 this.findRoutes 之前未完成...
  • 如何在调用 this.findRoutes() 之前等待 this.handleFormSubmit() 中的 this.setState() 完成?

低于标准的解决方案:

  • 将 this.findRoutes() 放在 componentDidUpdate() 中
  • 这是 Not Acceptable ,因为会有更多与 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!");
trở lại;
}
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);
} khác {
window.alert('Directions config failed due to ' + status);
}
});
});
return p1
},
kết xuất: hàm() {
trở lại (

Search


<>
onFormSubmit={this.handleFormSubmit}
map={this.state.map}/>
<>
setMapState={this.handleMapRender}
originId= {this.state.originId}
destinationId= {this.state.destinationId}
radius= {this.state.radius}
search= {this.state.search}/>

);
}
});

câu trả lời hay nhất

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() 调用中,作为第二个参数。
KHÔNG () 因为您正在传递函数。

关于javascript - 在 React 中触发函数之前如何等待 setState 完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401635/

25 4 0
Xem sitemap của VNExpress 之前?

在 MVC3 中,我可以轻松地将 jQuery 脚本标签移动到页面底部“_Layout.vbhtml” 但是,在 ASP.NET MVC3 中,当您使用编辑器模板创建 Controller 时,脚手

  • jquery - 返回到使用 .text() 之前

    悬停时内容被替换,但是当鼠标离开元素时我希望它变回来。我该怎么做? $('.img-wrap').hover(function(){ $(this).find('h4').text('Go

  • C 期望标识符或 ‘(’ 在 ‘{’ 之前

    Đã đóng cửa. Sự cố này không thể tái tạo được hoặc do lỗi chính tả. Câu trả lời không được chấp nhận vào thời điểm này. Sự cố này xảy ra do lỗi đánh máy hoặc sự cố không thể sao chép được nữa. Mặc dù một câu hỏi tương tự có thể được đặt lên hàng đầu

  • c - 预计在 elseif 之前

    Đã đóng cửa. Sự cố này không thể tái tạo được hoặc do lỗi chính tả. Câu trả lời không được chấp nhận vào thời điểm này. Sự cố này xảy ra do lỗi đánh máy hoặc sự cố không thể sao chép được nữa. Mặc dù một câu hỏi tương tự có thể được đặt lên hàng đầu

  • c - 预期说明符限定符列表位于 'if' 之前

    已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 有关您编写的代码问题的问题必须在问题本身中描述具体问题 - 并包含有效代码以重现该问题。

  • c++ - 预期类型说明符位于 'QwtLog10ScaleEngine' 之前?

    版本:qwt 6.0.1我尝试开发频谱的对数缩放。我使用简单的线条来启用缩放plotspectrum->setAxisScaleEngine(QwtPlot::yLeft, new QwtLog10S

  • c# - 插入触发器 SQL 之前

    我有两个相同的表,I_Subject 和 I_Temp_Subject,我想将 Temp_Subject 表复制到 Subject 表。 I_Temp_Subject 由简单用户使用,I_Subjec

  • javascript - 第一次绘制如何发生在 DOMContentLoaded 之前?

    我的印象是第一次绘制发生在触发 DOMContentLoaded 事件之后。特别是,因为我认为为了让第一次绘制发生,需要渲染树,它依赖于 DOM 构造。另外,我知道 DOM 构造完成时会触发 DOMC

  • IT王子
    Hồ sơ

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá taxi Didi miễn phí
    Phiếu giảm giá taxi Didi
    Các bài viết phổ biến trên toàn bộ trang web
    Chứng chỉ ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com