Updating state in React.js but there is a lag of one element
08:00 20 Nov 2025
    const onChangeFn = (e,i) => {
        const data = e.target.value;
        if(/^\d?$/.test(data)){
            let new_Array = [...values]
            new_Array[i] = data;
            setValues(new_Array);
            console.log(values)
        }
const [values, setValues] = useState(Array(number).fill(''))

The above function is being called by onChange attribute of an input element. I am using the console log to check my state variable, but the is a lag by one element.

javascript reactjs node.js react-hooks frontend