Cha*_*ack 3 measure react-native
我必须借助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
})
})
Run Code Online (Sandbox Code Playgroud)
如果需要测量更多的组件,它看起来就像一个回调地狱。
是否可以同步编写代码,例如在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}
Run Code Online (Sandbox Code Playgroud)
我找到了那种解决方案。
measure()不是承诺,但具有回调函数:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |