“在这里是未定义的是事件对象还是输入?”

huangapple go评论70阅读模式
英文:

what's undefined here Event Object or the inputs?

问题

The error message you're encountering, "Cannot read properties of undefined (reading 'value')" typically occurs when you try to access the value property of an undefined or null object.

In your code, this error may be related to how you're handling the input fields. Without a detailed analysis, it's challenging to pinpoint the exact issue. However, you mentioned that you want to create an OTP verification page with 6 input boxes where focus shifts to the next input element when you add a value and shifts back when you press backspace.

To achieve this, you can consider using controlled components in React to manage the input values and focus. You might also want to validate your inpRef.current elements to ensure they are properly initialized.

If you encounter specific issues or have more detailed questions about your code, please provide additional context or specific problems you'd like assistance with.

英文:

i have this sample code i was writing for OTP verification in Reactjs when i run this code the output shows an error that:

Cannot read properties of undefined (reading 'value')



import { useState, useEffect, useRef } from 'react'

import '../src/component.css'

export default function Component(){

  

  const[count,setCount] = useState(0);

  const inpRef = useRef([]);

  useEffect(()=>{

    

    setTimeout(()=>{

      setCount(count+1)

    },1000);

  

    const handleInput =(e,index)=>{

      const current = inpRef.current[index];

      let next = inpRef.current[index+1];

      let prev = inpRef.current[index-1];

      

     const expression = /^\d+$/

     const isValid = expression.test(e.target.value);

     

     if(!isValid){

       e.target.value = "";

       return;

     }

      if (e.target.value.length >1){

        e.target.value ="";

      }

     

    }

    

    inpRef.current.forEach((input,index)=>{

      input.addEventListener('input',handleInput.bind(null,index))

    })

   return ()=>{

     inpRef.current.forEach((input,index)=>{

      input.removeEventListener('input',handleInput.bind(null,index))

    })

   }

  },[])

  

  return(

    <>

    <p> Counter : {count}</p>

    <h4>Now enter the OTP</h4>

   

    <div className="whole">

    <h5>Send the OTP to your phone Number</h5>

     <div className="container">

    {[...Array(6)].map((_,index)=>{

      return <input className="text-field" type ="number"

      ref = {(ref) =>{

        inpRef.current[index] = ref;

      }} 

      key ={index}

      />

    })}

    </div>

    <button className ="btn">SUBMIT</button>

    </div>

    

    </>

    )


}

what am i doing wrong here? iwant an OTP verification page with 6input boxes so that whenever i put or add a value to an input field the focus keeps shifting to the next input element and when i press backspace the focus should shift back towards the previous element

答案1

得分: 0

你的 handleInput 函数的参数顺序错了,这里 indexevent,而 eindex。因此 e.target.valueundefined

const handleInput = (e, index) => { ... }

改为

const handleInput = (index, e) => { ... }

参考 codesanbox 示例

英文:

You have arguments of handleInput in wrong order, here index is event and e is index. Hence e.target.value is undefined.

change

const handleInput = (e, index) => { ... }

to

const handleInput = (index, e) => { ... }

see codesanbox example.

huangapple
  • 本文由 发表于 2023年5月13日 22:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定