I'm trying to focus a text input field once the user clicks a button. The code looks as follows:
const refName = React.useRef();const changeFocus = () => { refName.current.focus();}
with the text input having an attribute ref
defined as refName
.This gives me an error (undefined is not an object (evaluating 'refName.current.focus')
), and when I rewrite the changeFocus
function as console.log(refName)
this is what I get:
Object {"current": undefined,}
I'm not sure how else I'm supposed to define the reference or how else to approach this problem.Thanks