useImperativeHandle Explained with an Example | React Hooks Tutorial

preview_player
Показать описание

useImperativeHandle is explained with an example in this React hooks tutorial. useImperativeHandle is a React hook that should not be used often, but it does have some specific use cases. In this tutorial, we will look at how a web page modal can be one of the use cases.

useImperativeHandle Explained with an Example | React Hooks Tutorial

(00:00) Intro
(00:14) Welcome and setup
(00:35) What is useImperativeHandle?
(00:55) The parent component
(02:49) The modal component
(06:24) Rendering the modal and not the parent
(07:28) The docs and use cases for useImperativeHandle

🔗 ES7 React JS Snippets Extension for VS Code:

🔗 React Dev Tools Extension for Chrome:

📚 References:

✅ Follow Me:

Was this tutorial about the React hook useImperativeHandle helpful? If so, please share. Let me know your thoughts in the comments.

#useimperativehandle #react #hooks
Рекомендации по теме
Комментарии
Автор

Absolutely wonderful. This channel will be the most underrated channel on YouTube. Can't stress enough how useful this channel is for anyone trying to understand Strong foundational concepts. Will recommend this channel to everyone trying to get a career in Web Development.

Thank you, Dave. Keep on spreading knowledge!!!

redan
Автор

thanks Steve for this advanced concept, which make me confused at the beginning .

oussemabouyahia
Автор

This hook troubled me for 3 days. Finally got concept clearly. Thank you so much !!! Your videos helped me a lot :)

rishitiwari
Автор

Thank you Dave. It really helped me understand this hook. I look after various videos. But finally understood it after watching your video

gautam
Автор

This was the best explanation. Great content, glad I came across your channel.

joseoropeza
Автор

I recently ran into an issue where I had a button that lives outside the form, but they are both on the same parent imported as components. I initially used ref to trigger submit when the button is clicked but I also implemented this solution. I find my first solution to be less code and neat

oz
Автор

Thank you dave. Been enjoying your videos. Started watching your content from the node js series. it's really cool you have react content too! Thanks a bunch!

alvinacosta
Автор

I use it in multi-step form most of the time!

rafiurrahmanprotik
Автор

Thank you Dave for this amazing video. 😍

writelloyd
Автор

Am I understanding this correctly? In the alternative solution, passing the state setter as a prop, the state and setter would be located in the parent, so state changes would trigger re-renders in both the parent and child. In this proposed solution, the parent isn't re-rendering because modalState is located in the child. So as you said, the useImperativeHandle hook is used to define functions on a ref which is passed down to child components inside of the child component.

samuelchau
Автор

Thanks Dave, now i get it clearly :). Your tutorial helped me to avoid a lot of drilling :p and simplify the code.

maverick
Автор

Dave Gray checks in on December 24. Not only that, this is really interesting.

eleah
Автор

Hello, first, thank you for all your free videos . Are you planning to make an Angular full course ?

dosamuel-mb
Автор

I got here because I was finding a way to have a component that displays a stopwatch but I wanted it to have a local state and have it controlled by the parent component. I didn't want to lift the stopwatch state to the parent because it updates crazy fast. The parent component and its other children would re-render every second when the stopwatch starts which is pretty crazy.

warpatato
Автор

Interesting, but sometimes it's an overkill to do simple thing in a very well organized environment,
it's good to have a way to expose some inner functions to the parent component but what cases we would need to do that, that is the question,
Thanks Dave and I hope YT likes my comment this time 😁

ahmad-murery
Автор

Why not have modal context to avoid props drilling? I mean I understand how it works, I think that modals/pop-ups should be controlled within the app but is there any "best practice" way of using useImperativeHandle? I might think mb about some iframe exchange, if for example your iframe is a full fledged app but it might have few static controllers that might want to access via ref... but idk...Was really trying to find usecases for imperative handle or "best practices" with useTransition, useId, useDeferredValue instead of useDebounce....

eakzit
Автор

I didn't realise this existed. I have been passing a state hook as a prop so that I can set the state to a function which can be called from the parent component.
I'm not sure its worth rewriting my code to use useImperativeHandle instead. Can you think of a use case where using useImperativeHandle would be better?
I haven't been able to find any stale state issues.
Or perhaps I am misunderstanding the purpose of useImperativeHandle.

PsYcOtik
Автор

Dave, for this video your default language is korean... can't use english subtitles... otherwise everything is great as always.

tsf
welcome to shbcf.ru