PropTypes in React — React Technical Interview Questions & Answers
PropTypes in React
PropTypes in React
- PropTypes is a runtime checker you attach to a function or class: MyComp.propTypes = { … }. isRequired marks must-have props. defaultProps (or default parameters) fill gaps. You import from 'prop-types', not from 'react' (that split happened years ago).
- If the team already uses TypeScript, don’t add PropTypes ‘for extra safety’ on every file — you’ll maintain two sources of truth. Pick one.
- PropTypes in React — on screen: Pen × 1. Missing qty uses default. Wrong type → dev warning.
Technical Interview Tip: prop-types package + isRequired. TS in new code.
Code Examples for PropTypes in React
Example
// PropTypes + default
import PropTypes from "prop-types";
function Item({ title, qty }) {
return <li>{title} × {qty}</li>;
}
Item.propTypes = {
title: PropTypes.string.isRequired,
qty: PropTypes.number,
};
Item.defaultProps = { qty: 1 };
PropTypes in React — on screen: Pen × 1. Missing qty uses default. Wrong type → dev warning.
How to Explain PropTypes in React to an Interviewer
Explain PropTypes as if you are teaching a junior — definition, then one tiny UI.
Runtime prop checks (dev). prop-types.
What does the example show on screen (or print), and what does that prove?
PropTypes in React — on screen: Pen × 1
What mistake do freshers make with PropTypes?
TS usually replaces it.