Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call-child-function-from-parent-component-in-react/ #9

Open
utterances-bot opened this issue Mar 4, 2022 · 6 comments
Open

call-child-function-from-parent-component-in-react/ #9

utterances-bot opened this issue Mar 4, 2022 · 6 comments

Comments

@utterances-bot
Copy link

How To Call Child Function From Parent Component In React | Dilshan Kelsen

One way to call a child component's function from its parent is with the help of the useRef hook . Here's the gist of it: We pass a…

https://dilshankelsen.com/call-child-function-from-parent-component-in-react/

Copy link

Terro216 commented Mar 4, 2022

Thanks, nice example

Copy link

Thank you Dilshan, this worked for me.

Copy link

Works great, thank you!

Copy link

so it pass the alertUser function to parent using useEffect right?
but what if alertUser try to access some variable from the child component? will it work and why?

Copy link

Does this work with React 18 best practices https://beta.reactjs.org/learn/synchronizing-with-effects#

?

Copy link
Owner

dkelsen commented Feb 5, 2023

@muammarFaiz In that case, you would need add alertuser to the dependency array of useEffect, otherwise the function instance assigned to childFunc.current will reference stale variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants