【问题标题】:flow type React ref callback and createRef流类型 React ref 回调和 createRef
【发布时间】:2018-09-28 13:17:33
【问题描述】:
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。 Flow 会抱怨。

那么我应该如何正确输入我的Component 以便它适用于任何 HTML 元素?

【问题讨论】:

标签: reactjs flowtype


【解决方案1】:

我认为this 可能会解决您的问题。它还没有在文档中。

{current: null | React$ElementRef<ElementType>}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 2015-01-31
    • 2018-09-28
    • 2021-09-22
    • 2021-03-17
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多