【发布时间】:2020-02-05 07:58:43
【问题描述】:
我在一个类组件上使用了 this.refs,现在我将它重构为一个函数式组件。我正在使用 ViewShot 库:https://github.com/gre/react-native-view-shot 在之前的实现中,它的使用如下:
您的应用中有一个 QR 图像,并且您想在社交媒体上发送该图像,因此您将其包装在 ViewShot 中:
<ViewShot ref="viewShot">
<QRImage
address={....}
image={...}
/>
</ViewShot>
然后,当您单击共享图像时,它会执行以下操作:
onQRImagePress = async (address: string) => {
const viewShotRef: any = this.refs.viewShot;
try {
const uri = await viewShotRef.capture();
const options: Options = {
url: "file://" + uri,
title: `address: ${address}`,
};
await Share.open(options);
}
catch (e) {
console.log(`Could not screenshot the QR or share it due to: ${e}`);
}
};
所以我们使用类的 this.refs 来使用 ref。 我想为功能组件做这件事。最好使用挂钩。 我知道 userRef 存在,但它对我不起作用。我也尝试使用 createRef 但不确定如何正确实现它。
【问题讨论】:
标签: reactjs react-native react-hooks