import React, {useEffect, useRef, useState} from 'react'
import Container from '../Container'
import Title from '../form/Title'
import Submit from '../form/Submit'
const OTP_LENGTH = 6
export default function EmailVerification() {
const [otp,
setOtp] = useState(new Array(OTP_LENGTH).fill(""))
const[currentOtpIndex,setCurrentOtpIndex] = useState(0);
const inputRef = useRef();
useEffect(() => {
inputRef.current?.focus();
},[currentOtpIndex])
const handleOtpChange = ({target},ind) => {
const newOtp = [...otp] ;
const newValue = target.value;
newOtp[ind] = newValue.substring(newValue.length-1,newValue.length);
setCurrentOtpIndex(ind+1);
setOtp(newOtp);
}
return (
<div
className='fixed inset-0 bg-primary -z-10 flex justify-center items-center'>
<Container>
<form className='bg-secondary rounded p-6 space-y-6'>
<Title>Email Verification</Title>
<div className="flex space-x-6">
{otp.map((_, index) => {
return (<input
ref={(index === currentOtpIndex) ? inputRef:null}
key={index}
onChange={(e) => {handleOtpChange(e,index)}}
type="number"
className="w-12 h-12 outline-none rounded border-dark-subtle focus:border-white transition
bg-transparent text-xl font-semibold border-2 text-center text-white"/>)
})}
</div>
<Submit value="Verify"/>
</form>
</Container>
</div>
)
}
Dear All,
I am using this code to enter 6 digits of OTP for email verification. Here to prevent the user to be able to input multiple digits in any field, I am using a substring. This substring will get the last digit and update the current index of the field. When I am doing console.log, I am getting the correct values for OTP and newOtp, but on the screen, those changes are not reflected. That means, on screen, if I enter multiple digits in any field, i am not seeing the last digits but all the digits of that field.
Can you tell me what is wrong with my code ?