Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda