You never need to put a ref in as a dependency as they don't trigger a change in the same way.
A useEffect also has componentDidUnmount by returning a function. This can be vital when working with custom event listeners.
I've also rewritten the code in a way that makes it slightly easy to read (in my opinion) but I know that's just personal preference.
exportdefaultfunctionForm(){consttextInputRef=useRef(null);const[isEditing,setIsEditing]=useState(false);// Set focus if isEditing is trueuseEffect(()=>{const{current:input}=textInputRef;if(!isEditing||!input)return;input.focus()},[isEditing]);return(<div><inputplaceholder="Search"ref={textInputRef}type="text"disabled={!isEditing}/><buttononClick={()=>setIsEditing((prev)=>!prev)}>Toggle Focus</button></div>);};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There are a couple of things to mention here
useEffect
also hascomponentDidUnmount
by returning a function. This can be vital when working with custom event listeners.I've also rewritten the code in a way that makes it slightly easy to read (in my opinion) but I know that's just personal preference.