Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
useRef gives undefined in React Native but not in React

I'm trying to apply a mask for my input, i'm using useRef to get the current value, but it's always returning undefined, i'm running into an error, i tried the same code in React by only changing input to TextInput, and it's working. Here is my code

const [card, setCard] = useState();
const inputCard = useRef();

const handleChange = () => {
      const cardValue = inputCard.current.value
         .replace(/\D/g, "")
         .match(/(\d{0,4})(\d{0,4})(\d{0,4})(\d{0,4})/);
      inputCard.current.value = !cardValue[2]
         ? cardValue[1]
         : `${cardValue[1]}-${cardValue[2]}${`${cardValue[3] ? `-${cardValue[3]}` : ""}`}${`${
              cardValue[4] ? `-${cardValue[4]}` : ""
           }`}`;
      const numbers = inputCard.current.value.replace(/(\D)/g, "");
      setCard(numbers);
   };

useEffect(() => {
      handleChange();
   }, [card]);

and finally this is the input for React Native

<TextInput
            keyboardType='number-pad'
            ref={inputCard}
            onChangeText={handleChange}
         />

and this the input for React

<input
   ref={inputCard}
   onChange={handleChange}
/>

and this is the Error i'm getting in React Native

TypeError: undefined is not an object (evaluating 'inputCard.current.value.replace')
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!