【问题标题】:React forwardRef: How deep can you pass down forwardRef?React forwardRef:你可以向下传递多深的forwardRef?
【发布时间】:2020-05-28 08:56:24
【问题描述】:

我想知道您可以在嵌套应用程序中将 forwardRef 向下传递多深,以及如何将它向下传递?

类似这样的:

<Parent>
  <Child>
    ....
  </Child>
</Parent>

    <Child>
      <GrandChild>
          ....      
      </GrandChild>
    </Child>

        <GrandChild>
          <GreatGrandChild>
            ....
          </GreatGrandChild>
        </GrandChild>

            <GreatGrandChild>
              ....
            </GreatGrandChild>

然后在 GreatGrandChild 组件中使用它或将 Parent 中的 ref 和元素向下传递给 GreatGrandChild

【问题讨论】:

标签: reactjs ref react-forwardref


【解决方案1】:

您可以根据需要传递任意深度的 ref,但这似乎效率低下。

另一种方法是使用上下文 API 在生产者(包含 ref 元素的组件)中设置一个 ref,并在消费者(将使用该 ref 的子级)中使用它

【讨论】:

  • 您将如何在上下文中进行操作?抱歉,我无法解决这个问题。
猜你喜欢
  • 2021-08-28
  • 2019-11-07
  • 2022-12-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多