【发布时间】:2019-06-01 15:01:40
【问题描述】:
我正在尝试在 Promise 的 then() 中设置状态,但状态值没有得到保存,并且在 then() 之外无法访问。
下面是更新的代码:
handleSelect = location => {
this.setState({ location });
geocodeByAddress(
location
)
.then(results => getLatLng(results[0]))
.then(latLng => {
this.setState({
location_latitude: latLng.lat,
location_longitude: latLng.lng,
})
console.log(this.state.location_latitude); // returns the correct value
})
console.log(this.state.location_latitude); // returns null (the originally declared value)
}
如果我 console.log(this.state.location_latitude) 我得到一个 null 值。但是,如果我在 then() 块中使用 console.log,我会得到正确的值。这种情况下如何设置状态?
更新说明:
为了给这里的整个逻辑提供一个更好的上下文:我正在使用一个 Google Places 自动完成组件,它允许用户从下拉列表中选择一个位置。当用户从下拉列表中选择一个位置时,将调用上述方法handleSelect。然后我使用geocodeByAddress 方法提取用户选择的地点的纬度和经度,这是一个承诺。然后我需要检索纬度和经度并相应地设置状态,稍后用于发送到数据库。我还不能将值提交到数据库,因为用户需要填写其他表单输入(它们也存储在状态中),一旦他点击提交,我将提交所有状态元素单次调用后端。所以,我不想用componentDidMount() 更新任何内容,或者没有任何内容需要更新以使用componentDidUpdate()。无论如何,我只能将状态值存储在 geocodeByAddress 的 then 中(如果我在这里错了,请纠正我)。所以,我必须能够修改 then 块内的状态值。
【问题讨论】:
-
您的
console.log(location_latitude)记录为 null 在哪里? -
这个函数调用的其余代码在哪里?需要整个反应类方法的代码。在某处你丢失了
this上下文 -
如果您丢失了
this上下文,请不要忘记在构造函数中将您的函数绑定到this。 -
如果你试图访问这个状态来获得它的价值并首先在某个地方使用,你应该使用
ComponentDidMount。这样当您访问它时,该值就存在于状态中。
标签: javascript reactjs promise