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

112
Visualizações
PropTypes validation fails in functional component

I have a functional component below which has props coming from parent component and I added propTypes for the whole props object. However, the lint fails with the below error message.

9:16  error  'data' is missing in props validation                                   react/prop-types
9:22  error  'data.text' is missing in props validation

Excerpt from code

import PropTypes from 'prop-types'

const Child = (props) => <div>{props.data.text}</div>

Child.propTypes = {
 props: PropTypes.object
}

Could anyone please help?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As discussed in the comments, you have a few issues:

  1. PropTypes.object is a very vague type declaration, and most linters will ding you for that
  2. You are referencing properties of that object inside of your functional component that are not declared in your proptypes
  3. You are using your prop declaration to attempt to refer to the props argument as a whole, rather than the properties within.

A more correct way to write all of this would be like so:

import PropTypes from 'prop-types';

/* Note we destructure `data` directly from props in the args */
const Child = ({ data }) => (<div>{data.text}</div>);

Child.propTypes = {
  data: PropTypes.shape({ // use `shape` to allow us to declare types of properties within
    text: PropTypes.string, // define the text property's type
  }), 
}

Additionally, you may want to define some of these items as required, or provide default entries.

about 4 years ago · Juan Pablo Isaza Relatório

0

From Alexander Nied solution, I made it work writing the below way without using shape, I think it is more generic.

const Child = ({ data }) => (<div>{data.text}</div>);

Child.propTypes = {
  data: PropTypes.object
}
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