Sameer Kumar
1 min readSep 13, 2022

--

👋Hi Andrii. I understand your conflict. I’ll suggest not to look into class setting part as the core.

Looking closely, in this very example parent can select red or blue highlight without re rendering child due to prop change.

To answer your question, “Why we need imperative handle?”

> This useImperativeHandle is giving you ability to export custom methods from child to parent channelled through the ref, this opposite direction of flow needs this hook. React is a declarative library so you won’t find much use cases for this imperative hook. Use of refs is ONLY needed when you need real DOM based control outside react dom, setting focus is one such use case.


Apart from UI, it can be useful for some complex cases when you are interacting with libraries not written for react. One such recent use case at work came for using a webrtc based communication protocol, unfortunately these examples will be too complex for target audience. 😅

--

--

Sameer Kumar
Sameer Kumar

Written by Sameer Kumar

Working as Sr. Technical Consultant at Tarka Labs, India. I love Coding, Traveling and Teaching.

Responses (1)