我必须借助measure()函数来测量几个值。
因为它是异步操作,所以我只能写:
this.refContainerView.measure((x, y, width, height, pageX, pageY) => {
const containerViewHeight = height
this.refCommentList.measure((x, y, width, height, pageX, pageY) => {
const commentListOffset = pageY
const commentListHeight = height
// do something
})
})如果需要测量更多的组件,它看起来就像是回调地狱。
是否可以同步编写代码,例如,借助await或其他工具,例如:
const contaierView = this.refContainerView.measure()
const commentList = this.refCommentList.measure()
// and then do something with
contaierView {x, y, width, height, pageX, pageY}
commentList {x, y, width, height, pageX, pageY}发布于 2017-10-12 00:02:44
我找到了一种解决方案。
measure()不是promise,但有带回调的函数:
measureComponent = component => {
return new Promise((resolve, reject) => {
component.measure((x, y, width, height, pageX, pageY) => {
resolve({ x, y, width, height, pageX, pageY })
})
})
}
onDoSomething = async () => {
const [containerView, commentList] = await Promise.all([
this.measureComponent(this.refContainerView),
this.measureComponent(this.refCommentList),
])
// do here with containerView and commentList measures
}
}https://stackoverflow.com/questions/46685001
复制相似问题