简体   繁体   English

如何将祖父组件中的 function 传递给 ReactJs 中的孙子组件?

[英]How Can I pass a function in grandparent component to grandchild components in ReactJs?

I want to know how can I pass a function in grandparent component to grandchild components.我想知道如何将祖父组件中的 function 传递给孙子组件。
I have a function in Grandparent component which gets id and slice() that.我在祖父组件中有一个 function ,它获取idslice() But id comes from a grandchild.但是id来自孙子。
order is like this.顺序是这样的。

<Manager />
    <RecordList />
       <RecordItem />

when button clicked in, it should give id to the function which comes from单击按钮时,它应该为来自的 function 提供 id
How can I do that without Context API ?没有Context API怎么办?
and using pass props down to child并使用传递道具给孩子

could you please make an example?你能举个例子吗? Thank you all谢谢你们

You can use Context API in React and can pass any data to any child nester under a particular component.您可以在 React 中使用Context API并可以将任何数据传递给特定组件下的任何子嵌套器。

Context API React上下文 API 反应

EDIT Since you mentioned that you don't want to use Context API.编辑既然你提到你不想使用上下文 API。 For something like this, where the child is only one component deep, you can probably pass some props through the middle child.对于这样的事情,孩子只有一个组件深,您可能可以通过中间孩子传递一些道具。 I will give you some rough ideas as it's impossible to write the entire since you wrote no code in the example.我会给你一些粗略的想法,因为你在示例中没有写代码,所以不可能写完整。

<Manager>
    const childBtnClick = (params) => {
         // do you stuff here
    }
    <RecordList childBtnClick={childBtnClick}></RecordList>
</Manager>
<RecordList>
    <RecordItem childBtnClick={props.childBtnClick}></RecordItem>
</RecordList>
<RecordItem>
    <Button onClick={() => props.childBtnClick('passwhat data you want here')}></Button>
</RecordItem>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM