<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>React Archives | Mithle.sh</title>
	<atom:link href="https://mithle.sh/category/react/feed/" rel="self" type="application/rss+xml" />
	<link>https://mithle.sh/category/react/</link>
	<description>The Diary of a Full Stack Developer</description>
	<lastBuildDate>Tue, 14 Mar 2023 23:44:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>

<image>
	<url>https://i0.wp.com/mithle.sh/wp-content/uploads/2023/03/cropped-favicon.png?fit=32%2C32&#038;ssl=1</url>
	<title>React Archives | Mithle.sh</title>
	<link>https://mithle.sh/category/react/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">219607879</site>	<item>
		<title>How to Pass Data between Child and Parent Components in React</title>
		<link>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/</link>
					<comments>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/#respond</comments>
		
		<dc:creator><![CDATA[Mithlesh]]></dc:creator>
		<pubDate>Tue, 14 Mar 2023 11:43:18 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[frontend]]></category>
		<category><![CDATA[nextjs]]></category>
		<guid isPermaLink="false">https://blog.mithle.sh/?p=1233</guid>

					<description><![CDATA[<p>In this quick tutorial, we will learn how to pass data from the child component to the parent component and vice versa. Parent-to-Child Passing data...</p>
<p>The post <a href="https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/">How to Pass Data between Child and Parent Components in React</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this quick tutorial, we will learn how to pass data from the child component to the parent component and vice versa.</p>



<h2 class="wp-block-heading" id="parent-to-child">Parent-to-Child</h2>



<p>Passing data from parent to child component is very straightforward. You need to define <code>props</code> on the child component and then pass the values to it from the parent component. In the following example, we are rendering the avatar component (child) from the menu component(parent) by passing a <code>criminal</code> object to it.</p>



<h4 class="wp-block-heading" id="child-component">Child Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>interface IAvatarProps {
    avatarUrl: string;
    alt: string;
    title: string;
}
const Avatar = (props: IAvatarProps) =&gt; {
  return (
    &lt;img 
        src={props.avatarUrl} 
        title={props.title} alt={props.alt} 
        className="avatar" 
    /&gt;
  );
};

export default Avatar;
</code></pre>



<h4 class="wp-block-heading" id="parent-component">Parent Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>const criminal = {
        avatar: 'https://hindugenocide.com/islamic-jihad/9000-hindus-converted-in-madrasa/',
        name: 'Pir Ayub Jan Sarhandi'
    };
const Menu = () =&gt; {
    return (
        &lt;Menu&gt;
          &lt;Avatar 
              avatarUrl={criminal.avatar} 
              title={criminal.name} 
              alt="Hindu Genocide" 
          /&gt;
        &lt;/Menu&gt;
    );
};

export default Menu;
</code></pre>



<h2 class="wp-block-heading" id="child-to-parent">Child-to-Parent</h2>



<p>To pass the data from the child to its parent, we need to first define a <code>callback function</code> on the parent component which would then be passed via props on the child component. The child component would then call that function to pass data to the parent component.</p>



<p>For example, let us create a child component called <code>GenocideCard</code> which links to an article on <a href="https://hindugenocide.com/islamic-jihad/mirpur-massacre-of-hindus-and-sikhs-1947/" target="_blank" rel="noreferrer noopener">Mirpur Massacre.</a> We want that when this card is clicked, the article url should be passed to the parent component from where it is rendered.</p>



<ol>
<li>Define a callback function on the parent component called <code>handleGenocideCardClick</code> that accepts a parameter called <code>articleUrl</code>.</li>



<li>Pass that function in the <code>onCardClick</code> prop on the child component.</li>



<li>On the child component, we execute the <code>callback</code> function passed from the parent component when the <code>&lt;img&gt;</code> tag is clicked. The parameter of the <code>callback</code> function is passed as data to the parent component.</li>
</ol>



<h4 class="wp-block-heading" id="parent-component">Parent Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>const handleGenocideCardClick = (articleUrl) =&gt; {
    console.log("You can read about this massacre at: " + articleUrl);
}

const Layout = () =&gt; {
  return (
        &lt;Grid&gt;
          &lt;GenocideCard onCardClick={handleGenocideCardClick}/&gt;
        &lt;/Grid&gt;
    );
};

export default Layout;
</code></pre>



<h4 class="wp-block-heading" id="child-component">Child Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>interface IGenocideCardProps {
    onCardClick: any;
}
const HinduMassacre = {
      name: "Mirpur Massacre"
      image: "https://www.dailyexcelsior.com/wp-content/uploads/2020/11/1-176.jpg",
      url: "https://hindugenocide.com/islamic-jihad/mirpur-massacre-of-hindus-and-sikhs-1947/"
}
const GenocideCard = (props: IGenocideCardProps) =&gt; {
  return (
  &lt;div className="card"&gt;
    &lt;img 
        src={HinduMassacre.image} 
        title={HinduMassacre.name} 
        alt="Hindu Genocide" 
        onClick={() =&gt; props.onCardClick(HinduMassacre.url)} 
    /&gt;
    &lt;span class="text-red-400 font-medium"&gt;{HinduMassacre.name}&lt;/span&gt;
  );
};

export default GenocideCard;
</code></pre>
<p>The post <a href="https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/">How to Pass Data between Child and Parent Components in React</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1233</post-id>	</item>
	</channel>
</rss>
