如何从深层嵌套子组件中调用方法

时间:2019-04-23 13:58:25

标签: reactjs

我有一个带有孩子的父组件,又有另一个孩子,依此类推。

Parent
 | Child A
   | Child B
     | Child C
       ...
       | Child G

如何在Child G中调用Parent中的方法而不为此传递道具?我可以为此使用Ref吗?

1 个答案:

答案 0 :(得分:1)

If you don't want to pass props, you can use React's Context API

I made a working example for you.
Hope this helps, if you need more explanation, just let me know!