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

199
Vistas
How to enforce Array shape and size with Prop-Types?

I'm trying to create a proptype that can either be a string or an array with only 1 string and 1 object shape e.g. [10, { message: "hello world", type: "success" }]

Currently, I have:

static propTypes = {
    myProp: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.arrayOf(
            PropTypes.oneOfType([PropTypes.string, myObjectShape])
        ),
    ]),
 }

This will accept a string or an array that can have any amount of strings or object shapes. Is there a way to limit the array to only 1 string and 1 object shape?

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

0

You can create a custom prop type. Something along the lines of...

myProp: function(props, propName, componentName) {
    if (!props[propName] || !props[propName].length !== 2) {
      return new Error(`${propName} must be an array with length 2`);
    }

    if (typeof props[propName][0] !== 'string') {
      return new Error(`The first element in ${propName} must be a string`);
    }

    // check for the shape of the object at index 1...
}

More about it here.

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