Passing data in React between Parent and Child in Functional Components

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

  1. From Parent to Child
  2. From Child to Parent


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

No alt text provided for this image

Child.js

No alt text provided for this image

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.


  1. Create a callback method. This method will get the data from the Child to Parent.
  2. Pass your data as props in Child. The Child will call the Parent callback using props.
  3. The callback method in the Parent will act as prop to the Child component.


Here are the Child and Parent components (Link to Github Repo)

Parent.js

No alt text provided for this image


Child.js

No alt text provided for this image


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 .




Denys Stepaniuk

Front-end developer (JS, React, HTML5, CSS3 )

9 个月

Thank you very much ??

Juan Pablo Azambuyo

Web & Mobile Developer | React | React Native | Typescript | NodeJS

1 年

Thank you very much !! This saves me !!

Yuri Ancelmo

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 ?

Prabhjot Arora

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.

Syed Raza Ur Rehman Shah

Software Engineer | JavaScript | React Js | Angular | Node Js | Express Js | MongoDB | Web Developer | MySQl |MariaDB

1 年

You done a good job bro.

要查看或添加评论,请登录

Ivan T.的更多文章

  • Short hiatus (1 month) from social media and its impact

    Short hiatus (1 month) from social media and its impact

    I am a huge fan of LinkedIn, Instagram, TikTok, YouTube, Reddit, a few e-commerce sites and other popular platforms. I…

    15 条评论
  • Solution Focused - Not Knowing Position

    Solution Focused - Not Knowing Position

    Solution Focused Brief Therapy (SFBT) is a form of therapy that has gained popularity in recent decades. It is a brief…

    9 条评论
  • Text Counselling

    Text Counselling

    Why is counselling important for our mental health Counselling is important for mental wellness because it provides a…

    13 条评论
  • Loneliness

    Loneliness

    We feel lonely from time to time. The feelings of loneliness are personal, and everyone's experience will differ.

    9 条评论
  • Struggling over popular festive season (Lunar New Year)

    Struggling over popular festive season (Lunar New Year)

    Lunar New Year is this week. It is meant to be a time of joy with celebrations, red envelopes, gifts and qualify family…

    9 条评论
  • Getting your React project ready for?Heroku

    Getting your React project ready for?Heroku

    If you are new to React JS and want to deploy to Heroku, these are few steps to ensure that you can deploy…

    3 条评论
  • Console Tricks

    Console Tricks

    Console is is a favourite feature for many web developers. If you have been using it, you know there are lots of tricks.

    12 条评论
  • Why And How We Should Learn To Say?No

    Why And How We Should Learn To Say?No

    Are you a people pleaser ? Do you find it difficult to say "No" or reject an invitation, task or someone's request ?…

    9 条评论
  • HTTP Status Code

    HTTP Status Code

    When we visit a (web) site, it usually send some request to the server. There will be a returned code to indicate the…

    24 条评论
  • The Power of Stepping Away

    The Power of Stepping Away

    We have been there. Whether it is moving to a new department or in a unfamiliar role, we have encounter problems that…

    27 条评论

社区洞察

其他会员也浏览了