{"id":942,"date":"2023-06-12T17:30:45","date_gmt":"2023-06-12T17:30:45","guid":{"rendered":"https:\/\/s-oneill.com\/sov02\/?p=942"},"modified":"2023-06-12T18:19:42","modified_gmt":"2023-06-12T18:19:42","slug":"react-passing-data-up-to-a-grandparent","status":"publish","type":"post","link":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/","title":{"rendered":"React: Passing Data Up To a GrandParent"},"content":{"rendered":"<p>In React, the general rule is: data flows in one direction &#8211; down. Without getting into data sharing processes like Context or Redux, generally speaking, in React, data flows down from parents to children. That is one of the things that makes React so efficient.<\/p>\n<p>However, data can in effect be &#8220;passed up&#8221; to a Parent using a callback function defined in the Parent. That callback is passed down to the Child as a prop using a custom &#8220;on&#8221; event key name, like onDoCallback. <\/p>\n<p>For example, a callback function called doCallback() defined in the Parent can be passed down to the Child as a prop like this:<br \/>\n\t<code>&nbsp; &nbsp; &lt;Child onDoCallback={doCallback} \/><\/code><\/p>\n<p>The Child component function then takes in the custom &#8220;on&#8221; event name { onDoCallback } as a prop, and uses it to execute the callback function defined in the Parent. So in the Child, calling onDoCallback(data) is in effect, executing the function doCallback(data) in the Parent. The (data) parameter is provided to the Parent function from the Child, so you could say, data is being passed up to the Parent.<\/p>\n<p>It can look something like this:<\/p>\n<p><strong>Parent.js<\/strong><\/p>\n<p>  <code>const [parentData, setParentData]=useState(\"\")<br \/>\n    function Parent() {<br \/>\n      &nbsp; function <strong>doCallback(data)<\/strong> {<br \/>\n        &nbsp; &nbsp; setParentData(data)<br \/>\n      &nbsp; }<br \/>\n      &nbsp; return (<br \/>\n        &nbsp; &nbsp; &lt;div ><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;Child <strong>onDoCallback={doCallback}<\/strong> \/><br \/>\n        &nbsp; &nbsp; &lt;\/div><br \/>\n      &nbsp; )<br \/>\n    }<br \/>\n    export default Parent;<\/code><\/p>\n<p><strong>Child.js<\/strong><\/p>\n<p> <code> function Child(<strong>{ onDoCallback }<\/strong>) {<br \/>\n    const [formData, setFormData]=useState(\"\")<br \/>\n    function handleSubmit(event) {<br \/>\n      &nbsp; event.preventDefault()<br \/>\n      &nbsp; &nbsp; const newData={<br \/>\n        &nbsp; &nbsp; &nbsp; formData<br \/>\n      &nbsp; &nbsp; }<br \/>\n      &nbsp; &nbsp; <strong>onDoCallback(newData)<\/strong><br \/>\n    &nbsp; }<br \/>\n    &nbsp; return (<br \/>\n      &nbsp; &nbsp; &lt;div><br \/>\n        &nbsp; &nbsp; &nbsp; &lt;form onSubmit={handleSubmit}><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;input type=\"text\" id=\"formData\"<br \/>\n\t    &nbsp; &nbsp; &nbsp; value={formData}<br \/>\n\t    &nbsp; &nbsp; &nbsp; onChange={e => setFormData(e.target.value)}<br \/>\n\t &nbsp; &nbsp; &nbsp; \/><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;button type=\"submit\">Add Data&lt;\/button><br \/>\n        &nbsp; &nbsp; &lt;\/form><br \/>\n      &nbsp; &nbsp; &lt;\/div><br \/>\n    &nbsp; )<br \/>\n  }<br \/>\n  export default Child;<\/code><\/p>\n<p>When the form is submitted in the Child, the Child&#8217;s newData is passed to the doCallback function in the Parent, which then can be used to update state. <\/p>\n<p>&nbsp;<\/p>\n<h3>But what about passing data from a Child to a  <em>Grandparent<\/em>  (or GreatGrandparent)? <\/h3>\n<p>The process is similar, but slightly different. <\/p>\n<p>In this case, like the previous example, the doCallback() function is defined in the top component, let&#8217;s say the Grandparent. The Grandparent passes the doCallback() to it&#8217;s child (Parent) as a prop using a custom &#8220;on&#8221; event keyname, much like the previous example: <\/p>\n<p>\t<code>&nbsp; &nbsp; &lt;Parent onDoCallback={doCallback} \/><\/code><\/p>\n<p>The Parent component function takes { onDoCallback } as a prop, and simply passes it to the child component. But notice there is one subtle difference in the way it is passed. Instead of using onDoCallback={doCallback}, the Parent uses onDoCallback={onDoCallback} The key and the value are the same value.<\/p>\n<p>\t<code>&nbsp; &nbsp; &lt;Child <strong>onDoCallback={onDoCallback}<\/strong> \/><\/code><\/p>\n<p>So it would look something like this:<\/p>\n<p><strong>GrandParent.js<\/strong><\/p>\n<p>   <code> const [grandParentData, setGrandParentData]=useState(\"\")<br \/>\n    function GrandParent() {<br \/>\n      &nbsp; function <strong>doCallback(data)<\/strong> {<br \/>\n        &nbsp; &nbsp; setGrandParentData(data)<br \/>\n      &nbsp; }<br \/>\n      &nbsp; return (<br \/>\n        &nbsp; &nbsp; &lt;div ><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;Parent <strong>onDoCallback={doCallback}<\/strong> \/><br \/>\n        &nbsp; &nbsp; &lt;\/div><br \/>\n      &nbsp; )<br \/>\n    }<br \/>\n    export default Parent;<\/code><\/p>\n<p><strong>Parent.js<\/strong><\/p>\n<p>   <code> function Parent(<strong>{ onDoCallback }<\/strong>) {<br \/>\n      &nbsp; \/\/ other Parent code<br \/>\n      &nbsp; return (<br \/>\n        &nbsp; &nbsp; &lt;div ><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;Child <strong>onDoCallback={onDoCallback}<\/strong> \/><br \/>\n        &nbsp; &nbsp; &lt;\/div><br \/>\n      &nbsp; )<br \/>\n    }<br \/>\n    export default Parent;<\/code><\/p>\n<p><strong>Child.js<\/strong><br \/>\n  <code>function Child(<strong>{ onDoCallback }<\/strong>) {<br \/>\n    const [formData, setFormData]=useState(\"\")<br \/>\n    function handleSubmit(event) {<br \/>\n      &nbsp; event.preventDefault()<br \/>\n      &nbsp; const newData={<br \/>\n        &nbsp; &nbsp; formData<br \/>\n      &nbsp; }<br \/>\n      &nbsp; <strong>onDoCallback(newData)<\/strong><br \/>\n    }<br \/>\n    return (<br \/>\n      &nbsp; &lt;div><br \/>\n       &nbsp; &nbsp;  &lt;form onSubmit={handleSubmit}><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;input type=\"text\" id=\"formData\"<br \/>\n\t    &nbsp; &nbsp; &nbsp; value={formData}<br \/>\n\t    &nbsp; &nbsp; &nbsp; onChange={e => setFormData(e.target.value)}<br \/>\n\t  &nbsp; &nbsp; &nbsp; \/><br \/>\n          &nbsp; &nbsp; &nbsp; &lt;button type=\"submit\">Add Data&lt;\/button><br \/>\n        &nbsp; &nbsp; &lt;\/form><br \/>\n      &nbsp; &lt;\/div><br \/>\n   )<br \/>\n  }<br \/>\n  export default Child;<\/code><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Now, when the Child fires off onDoCallback(newData)<br \/>\nit is, in effect, passing data from Child to a function in the GrandParent.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In React, the general rule is: data flows in one direction &#8211; down. Without getting into data sharing processes like Context or Redux, generally speaking, in React, data flows down from parents to children. That is one of the things that makes React so efficient. However, data can in effect be &#8220;passed up&#8221; to a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-942","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React: Passing Data Up To a GrandParent - s-oneill.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React: Passing Data Up To a GrandParent - s-oneill.com\" \/>\n<meta property=\"og:description\" content=\"In React, the general rule is: data flows in one direction &#8211; down. Without getting into data sharing processes like Context or Redux, generally speaking, in React, data flows down from parents to children. That is one of the things that makes React so efficient. However, data can in effect be &#8220;passed up&#8221; to a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\" \/>\n<meta property=\"og:site_name\" content=\"s-oneill.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-12T17:30:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-12T18:19:42+00:00\" \/>\n<meta name=\"author\" content=\"Me(sean)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Me(sean)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\"},\"author\":{\"name\":\"Me(sean)\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/ab11148c6ea273be80dddd8baec1a545\"},\"headline\":\"React: Passing Data Up To a GrandParent\",\"datePublished\":\"2023-06-12T17:30:45+00:00\",\"dateModified\":\"2023-06-12T18:19:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\"},\"wordCount\":373,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\",\"url\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\",\"name\":\"React: Passing Data Up To a GrandParent - s-oneill.com\",\"isPartOf\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/#website\"},\"datePublished\":\"2023-06-12T17:30:45+00:00\",\"dateModified\":\"2023-06-12T18:19:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/s-oneill.com\/sov02\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React: Passing Data Up To a GrandParent\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/#website\",\"url\":\"https:\/\/s-oneill.com\/sov02\/\",\"name\":\"s-oneill.com\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/s-oneill.com\/sov02\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a\",\"name\":\"sean o\",\"logo\":{\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/image\/\"},\"description\":\"Web dev over 15 years.\",\"sameAs\":[\"http:\/\/s-oneill.com\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/ab11148c6ea273be80dddd8baec1a545\",\"name\":\"Me(sean)\",\"url\":\"https:\/\/s-oneill.com\/sov02\/author\/user01\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React: Passing Data Up To a GrandParent - s-oneill.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/","og_locale":"en_US","og_type":"article","og_title":"React: Passing Data Up To a GrandParent - s-oneill.com","og_description":"In React, the general rule is: data flows in one direction &#8211; down. Without getting into data sharing processes like Context or Redux, generally speaking, in React, data flows down from parents to children. That is one of the things that makes React so efficient. However, data can in effect be &#8220;passed up&#8221; to a [&hellip;]","og_url":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/","og_site_name":"s-oneill.com","article_published_time":"2023-06-12T17:30:45+00:00","article_modified_time":"2023-06-12T18:19:42+00:00","author":"Me(sean)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Me(sean)","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#article","isPartOf":{"@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/"},"author":{"name":"Me(sean)","@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/ab11148c6ea273be80dddd8baec1a545"},"headline":"React: Passing Data Up To a GrandParent","datePublished":"2023-06-12T17:30:45+00:00","dateModified":"2023-06-12T18:19:42+00:00","mainEntityOfPage":{"@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/"},"wordCount":373,"commentCount":0,"publisher":{"@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/","url":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/","name":"React: Passing Data Up To a GrandParent - s-oneill.com","isPartOf":{"@id":"https:\/\/s-oneill.com\/sov02\/#website"},"datePublished":"2023-06-12T17:30:45+00:00","dateModified":"2023-06-12T18:19:42+00:00","breadcrumb":{"@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/s-oneill.com\/sov02\/react-passing-data-up-to-a-grandparent\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/s-oneill.com\/sov02\/"},{"@type":"ListItem","position":2,"name":"React: Passing Data Up To a GrandParent"}]},{"@type":"WebSite","@id":"https:\/\/s-oneill.com\/sov02\/#website","url":"https:\/\/s-oneill.com\/sov02\/","name":"s-oneill.com","description":"","publisher":{"@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/s-oneill.com\/sov02\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/a9217b5797251a243dfd504d7ad7d00a","name":"sean o","logo":{"@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/image\/"},"description":"Web dev over 15 years.","sameAs":["http:\/\/s-oneill.com"]},{"@type":"Person","@id":"https:\/\/s-oneill.com\/sov02\/#\/schema\/person\/ab11148c6ea273be80dddd8baec1a545","name":"Me(sean)","url":"https:\/\/s-oneill.com\/sov02\/author\/user01\/"}]}},"_links":{"self":[{"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/posts\/942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/comments?post=942"}],"version-history":[{"count":15,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/posts\/942\/revisions\/957"}],"wp:attachment":[{"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/media?parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/categories?post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/s-oneill.com\/sov02\/wp-json\/wp\/v2\/tags?post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}