{"id":109,"date":"2024-08-19T06:50:03","date_gmt":"2024-08-19T06:50:03","guid":{"rendered":"https:\/\/www.yehger.com\/?p=109"},"modified":"2024-08-19T06:52:56","modified_gmt":"2024-08-19T06:52:56","slug":"react-how-to-call-child-function","status":"publish","type":"post","link":"https:\/\/www.yehger.com\/index.php\/2024\/08\/19\/react-how-to-call-child-function\/","title":{"rendered":"React how to call child function?"},"content":{"rendered":"<h2>Use the &quot;useImperativeHandle&quot;<\/h2>\n<p><strong>Parent file:<\/strong><\/p>\n<pre><code class=\"language-jsx\">import {useRef,useEffect} from &#039;react&#039;\nimport RankingPopup from &#039;@\/components\/popups\/Ranking&#039;\n\nexport default function(){\n\n    const RankingPopupRef = useRef(null)\n\n    useEffect(()=&gt;{\n        RankingPopupRef.current.open()\n    },[])\n    return &lt;RankingPopup ref={RankingPopupRef} \/&gt;\n}<\/code><\/pre>\n<p><strong>Child file:<\/strong><\/p>\n<pre><code class=\"language-jsx\">import * as React from &#039;react&#039;\n\nexport default React.forwardRef(({\n    data=[],\n    self={}\n},ref)=&gt;{\n\n    React.useImperativeHandle(ref,()=&gt;({\n        open(){\n            console.log(&quot;called child function&quot;)\n        }\n    }))\n\n    return &quot;child fun&quot;\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use the &quot;useImperativeHandle&quot; Parent file: import {useRef,useEffect} from &#039;react&#039; import RankingPopup from &#039;@\/components\/popups\/Ranking&#039; export default function(){ const RankingPopupRef = useRef(null)&#8230; <a class=\"read-more\" href=\"https:\/\/www.yehger.com\/index.php\/2024\/08\/19\/react-how-to-call-child-function\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-react-study"],"_links":{"self":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":1,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}