React Easy technical 0 views 2 min read

How to apply validation on props in React?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

When the application is running in _development mode_, React will automatically check all props that we set on components to make sure they have _correct type_. If the type is incorrect, React will generate warning messages in the console. It's disabled in _production mode_ due to performance impact. The mandatory props are defined with isRequired.

The set of predefined prop types:

  1. PropTypes.number
  2. PropTypes.string
  3. PropTypes.array
  4. PropTypes.object
  5. PropTypes.func
  6. PropTypes.node
  7. PropTypes.element
  8. PropTypes.bool
  9. PropTypes.symbol
  10. PropTypes.any

We can define propTypes for User component as below:

```jsx harmony
import React from "react";
import PropTypes from "prop-types";

class User extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired,
};

render() {
return (
<>
<h1>{Welcome, ${this.props.name}}</h1>
<h2>{Age, ${this.props.age}}</h2>
</>
);
}
}


    **Note:** In React v15.5 _PropTypes_ were moved from `React.PropTypes` to `prop-types` library.
    
    **Modern Recommendation:** While PropTypes are still supported, **TypeScript** is now the industry standard for type checking in React applications. Consider using TypeScript for better type safety, IDE support, and compile-time error detection.

    _The Equivalent Functional Component_

    
jsx harmony
import React from "react";
import PropTypes from "prop-types";

function User({ name, age }) {
return (
<>
<h1>{Welcome, ${name}}</h1>
<h2>{Age, ${age}}</h2>
</>
);
}

User.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired,
};

    
    _Modern TypeScript Version_

    
tsx
import React from "react";

interface UserProps {
name: string;
age: number;
}

function User({ name, age }: UserProps) {
return (
<>
<h1>{Welcome, ${name}}</h1>
<h2>{Age, ${age}}</h2>
</>
);
}
```

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Spotted an error or have an alternative solution?