Passing data in React between Parent and Child in Functional Components
For beginners who started out in ReactJS, passing data between components may be confusing. I struggle this when I first started out. There are two ways of passing data in React components
Passing Data from Parent To Child Components
When you are passing data from, you can use props to transfer the data. The Child will access the data in the props.
An example like this where you want to transfer a message from Parent to Child.
We have the Parent and Child components (Link to Github Repo)
Parent.js
Child.js
The Parent pass the data in message, and the Child got the data in props.message. Props pass the state from Parent to Child.
Passing Data from Child To Parent Components
Passing data from child to parent is more tricky. This is different compare to passing data from Parent to Child.
Here are the Child and Parent components (Link to Github Repo)
Parent.js
Child.js
Conclusion
We learn how to pass data between Parent and Child in functional component. Knowing this is essential to build an app in ReactJS. The Github repo is here .
Front-end developer (JS, React, HTML5, CSS3 )
9 个月Thank you very much ??
Web & Mobile Developer | React | React Native | Typescript | NodeJS
1 年Thank you very much !! This saves me !!
Remote-only | Senior Software Developer | .Net Core 6.0 | React.js | AWS | WPF | SQL
1 年I appreciate your article, and I found it using Google, when I was looking for a solution to pass a event click from the parent to the child. So in cases that you have a FabButton on parent component, and you click on it, I'm looking for a way to notify children to do something with this information. What will you think that is the best approach ?
Intern @ BAOIAM | Ex-Intern @ ZealYug | Full Stack Developer | MCA Student
1 年Amazing...I looked around stackoverflow and other platforms for Child To Parent Data Passing, but didn't understand. After I saw this, it's totally clear. Thank you for making this so simple.
Software Engineer | JavaScript | React Js | Angular | Node Js | Express Js | MongoDB | Web Developer | MySQl |MariaDB
1 年You done a good job bro.