[英]flow type React ref callback and createRef
class Component extends React.Component<ComponentProps> {
// ...magic
elementRef = React.createRef();
render() {
return this.props.children(this.elementRef);
}
}
這是我的一個組成部分。 它基本上將ref回調傳遞給了孩子們。 然后就像這樣使用它
<Component>
{elementRef => (
<div ref={elementRef} />
)}
</Component>
我的Component
的意圖是孩子們可以將elementRef
附加到任何元素。
但是我遇到了很多麻煩,無法正確輸入我的組件。
我現在在做什么:
type ComponentProps = {
+children: (
ref: { current: HTMLDivElement | null } | ((ref: ?HTMLDivElement) => void)
) => React.Node,
};
但顯然這只適用於div
。 如果我在跨度上使用elementRef。 流會抱怨。
那么我應該如何正確鍵入我的Component
,以便它適用於任何HTML元素?
我認為這可以解決你的問題。 它尚未出現在文檔中。
{current: null | React$ElementRef<ElementType>}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.