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

116
Vistas
how to set button disable in certain condition in react?
import PhoneInput, { formatPhoneNumber, formatPhoneNumberIntl, isValidPhoneNumber } from 'react-phone-number-input';

const [fields, setFields] = React.useState([{ value: null }]);

<PhoneInput
  id="phoneNumbers1"
  type="text"
  placeholder="Enter phone number "
  value={field.value || ""}
  defaultCountry="US"
  international
  name="phoneNumbers"
  error={disable}
  autoComplete='off'
  onBlur={formik.handleBlur}
  onChange={(e) => handleChange(idx, e)}
  onClick={() => setIndex(idx)}
/>
<button
  type="button"
  hidden={fields.length > 1}
  data-value={fields.length}
  disabled={!fields[0]?.value && !(isValidPhoneNumber(fields[0]?.value ||''||fields.length<10))}
  onClick={() => handleAdd()}
>
  + Phone
</button>

Inside button inside disable property if I try to give condition that button is disabled until user give 10 digit phone number with condition field.length < 10. It throws an error "TypeError: A text for parsing must be a string." What should be the condition inside disable attribute that until user give 10 digit phone number button should disable?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I had to guess here, it seems isValidPhoneNumber may be expecting a string value, but if fields[0]?.value is falsey, and '' is obviously falsey, the boolean fields.length < 10 value is passed. I suspect you want this last condition outside of the isValidPhoneNumber validator function. I also suspect you want to check the length of the value, not the length of the fields array.

disabled={
  !isValidPhoneNumber(fields[0]?.value || "") ||
  fields[0]?.value?.length < 10
}

Edit how-to-set-button-disable-in-certain-condition-in-react

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think the last condition is checking the length of array instead the length of the first value of the fields array i.e. fields[0].value.

Try to use

disabled={!fields[0]?.value && !(isValidPhoneNumber(fields[0]?.value ||''||fields[0]?.value.length<10))}

about 4 years ago · Juan Pablo Isaza Denunciar
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