2017-10-11 13 views
0

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} 

답변

0

해결책을 찾았습니다.
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 
    } 
}