In the following example, what will happen when we click on the “Magic” button (button with text Magic)?
import React, { useRef, forwardRef, useImperativeHandle } from 'react'; const Input = forwardRef((props, ref) => { const inputRef = useRef(); useImperativeHandle(ref, () => ({ doMagic: () => { inputRef.current.focus(); }, })); return <input ref={inputRef} />; }); function App() { const parentRef = useRef(); return ( <div> <Input ref={parentRef} /> <button onClick={() => parentRef.current.doMagic()}>Magic</button> </div> ); }
This content is only available to registered users.Please register at the the home page.