Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!