获取组件的位置信息
2022-04-15 本文已影响0人
sunny635533
<TouchableOpacity onPress={() => {
const ref = this.fileMoreBtnRef[index];
if (ref) {
ref.measure((x, y, width, height, pageX, pageY) => {
console.log('view.measure x = ', x);
console.log('view.measure y = ', y);
console.log('view.measure width = ', width);
console.log('view.measure height = ', height);
console.log('view.measure left = ', pageX);
console.log('view.measure top = ', pageY);
})
}
}}>
<Image ref={(btnRef) => this.fileMoreBtnRef.push(btnRef)} source={more_circle} imageStyle={{ marginHorizontal: 12 }} />
</TouchableOpacity>
注意,自定义封装的Component是没有mearsure方法的,要参考官网那样取出Ref,如下
class ParentComponent extends React.Component<Props> {
_ref: ?React.ElementRef<typeof ChildComponent>;
render() {
return <ChildComponent ref={this._captureRef} onSubmit={this._onSubmit} />
}
_captureRef: (ref) => {
this._ref = ref;
}
_onSubmit: () => {
if (this._ref != null)
this._ref.getViewRef().measure(() => {});
}
}
}
class ChildComponent extends React.Component<Props> {
_ref: ?React.ElementRef<typeof View>;
render() {
return (
<View ref={this._captureRef}>
<SubmitButton onSubmit={props.onSubmit} />
</View>
);
}
getViewRef(): ?React.ElementRef<typeof View> {
return this._ref;
}
_captureRef: (ref) => {
this._ref = ref;
}
}