使用结构从父级调用函数。<App><Parent><Child></Child></Parent><App>



我是新手。基本上,我希望当我点击子组件的按钮时,考虑到这个结构:

<Parent>
<Child/>
<Parent>

从父组件调用函数CCD_ 1。我该怎么做?

这是我的实时代码

import "./styles.css";
export const Parent = ({ children }) => {
//I need excecute myFunction when user click
return (
<div>
I am parent <br />
<br /> {children}
</div>
);
};
export const Child = () => {
const myFunction = () => {
console.log("say hello from parent");
};
return (
<div>
I am children
<br />
<button>Send function to execute in the parent</button>
</div>
);
};
export default function App() {
return (
<Parent>
<Child />
</Parent>
);
}

您需要的是将回调函数prop传递给子组件

查看此处从父到子的回调函数

最新更新