繁体   English   中英

从 forwardRef 中提取 ref(createRef 与 useRef 相比)| 反应原生

[英]Extracting the ref from the forwardRef (createRef compared to useRef) | react native

我想让我们 react-native-share 共享一个图像,它是我的应用程序中的一个视图(使用 ViewShot),这并不重要。

当我使用 useRef 获取我想要共享的视图的 ref 时,我已经成功使用了一段时间。 打印 current.props.children 时,我看到:

<ShareQRImage prop1="..." />}

这是我想要的观点,而且效果很好。

现在我不能使用 useRef 因为我不在功能组件中。 我想导出参考。 我正在为图像使用 createRef。 我得到了 ref 但在打印 current.props.children 时我得到:

<ForwardRef(Image) source={172} />

我现在想得到的是

<Image source={172} />

我如何从 forwardRef 得到这个?

forwardRef 是您漏看的部分! 来自 reactjs.org 的这个代码片段可以帮助你:

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM