How to change focus/enable/disable textbox in child component based on parent button click?
Control a child input element’s focus or disabled state from its parent component using useRef and state.
intermediateComponent communicationreactuseRefparent-child communicationdom control
Published: 10/26/2025
Updated: 10/26/2025
Question
How to change focus/enable/disable textbox in a child component based on parent button click?
Answer
Use refs to expose child DOM nodes to the parent, then manipulate focus or disabled states.
import { useRef, forwardRef, useImperativeHandle } from 'react';
const ChildInput = forwardRef((props, ref) => {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => inputRef.current.focus(),
disable: () => (inputRef.current.disabled = true),
enable: () => (inputRef.current.disabled = false),
}));
return <input ref={inputRef} placeholder='Type here...' />;
});
export default function Parent() {
const childRef = useRef();
return (
<div>
<ChildInput ref={childRef} />
<button onClick={() => childRef.current.focus()}>Focus</button>
<button onClick={() => childRef.current.disable()}>Disable</button>
<button onClick={() => childRef.current.enable()}>Enable</button>
</div>
);
}
âś… The parent can now directly call focus(), disable(), or enable() on the child.
Real-World Example
- Autofocusing search fields
- Enabling form fields after validation
- Triggering UI actions across components
Quick Practice
Build a parent/child pair where a “Reset” button in the parent clears the child input field.
Summary
Use useRef with forwardRef + useImperativeHandle to let parents control child component DOM behavior.
Frequently Asked Questions
Can I directly access child DOM from parent?
Yes, using forwardRef or callback refs to expose the element reference.
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.