Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda