简体   繁体   English

如何将道具从子组件传递给父组件?

[英]How to pass props from child component to parent?

I have to pass props from child component to parent, I read the documentation it says passing props is just one-way flow.我必须将道具从子组件传递给父组件,我阅读了它说传递道具只是单向流的文档。 I guess there is a workaround but I can't figure it out.我想有一个解决方法,但我无法弄清楚。

Yes, there is you have to pass a function to the child component as props.是的,您必须将 function 作为道具传递给子组件。

var getData = (data) => {
  console.log(data);
}


<Child data={this.getData} />

then in Parent Component execute this function and pass the data as an argument.然后在父组件中执行此 function 并将数据作为参数传递。 The function will execute in the parent component and displayed in the console. function 将在父组件中执行并显示在控制台中。 You can use that data from there either by setState or as you wish.您可以通过 setState 或根据需要从那里使用该数据。

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

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