reactjs - React forwardRef:你可以向下传递多深的forwardRef?
问题描述
我想知道您可以在嵌套应用程序中将 forwardRef 向下传递多深,以及如何将它传递下去?
像这样的东西:
<Parent>
<Child>
....
</Child>
</Parent>
<Child>
<GrandChild>
....
</GrandChild>
</Child>
<GrandChild>
<GreatGrandChild>
....
</GreatGrandChild>
</GrandChild>
<GreatGrandChild>
....
</GreatGrandChild>
然后在 GreatGrandChild 组件中使用它或将 Parent 中的 ref 和元素向下传递给 GreatGrandChild
解决方案
您可以根据需要传递任意深度的 ref,但这似乎效率低下。
一种替代方法是使用上下文 API 在生产者(包含 ref 元素的组件)中设置一个 ref 并在消费者(将使用该 ref 的子级)中使用它
推荐阅读
- mongodb - 克隆 MongoDB 以运行测试
- html - 如何使整个浮动元素块与其父容器的中心对齐?
- jquery - 如何在“响应的图形摘要”Sharepoint 调查页面上隐藏特定问题?
- xml - 如何在python中使用ElementTree在一个程序中编写多个xml文件
- vuejs2 - NuxtJS/apollo 如何设置自定义标头
- php - php 使用 strtolower('Fötus') 的奇怪编码行为;
- google-sheets - 跨多个工作表的 Google 工作表查询
- webpack - Webpack 4 - 复制 dub 目录结构
- python - 如何在 Redis 缓存中附加字典?
- python - 解析方法的 Scrapy 参数