React useRef is easy!

RMAG news

We can use useRef instead of the usual way (event.target.value) to fetch an input value, in a much easier hassle free way. This example below can make you familiar with useRef, enabling you to sue it for more difficult scenarios:

const unameRef = React.useRef(); //declaration

const [userName, setUserName] = React.useState(”); // I have a sample variable to set username to

// Usage – React element

// handleChange function

function handleChange(event){
setUserName(unameRef.current.value);
}