← Back to React Course | Chapter 3: Components & Props | Lesson 6 of 11

PropTypes से Props की Type-Checking

PropTypes एक checklist जैसी हैं जो warn करती हैं अगर आपने एक component को गलत kind की ingredient दी हो।
Syntax
markup
import PropTypes from "prop-types";

ComponentName.propTypes = {
  propName: PropTypes.string.isRequired,
  other: PropTypes.number,
};

PropTypes Declare करना

prop-types package से PropTypes import करने के बाद, आप अपने component पर एक propTypes object attach करते हैं जो हर prop की expected type list करता है।

Development के दौरान, React incoming props को इसके against check करता है और अगर कुछ match न हो तो एक warning log करता है।

Note: उन props को जो हमेशा provided होने चाहिए .isRequired से mark करें, जैसे PropTypes.string.isRequired।
Warning: PropTypes warnings सिर्फ browser console में दिखती हैं -- वे कभी आपके app को render होने से नहीं रोकती।

उदाहरण: Declaring PropTypes

markup
// Run in your local React project (npm install required)
import PropTypes from 'prop-types';

function Greeting({ name, age }) {
  return <p>{name} is {age} years old.</p>;
}

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

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Common PropTypes Validators

prop-types library सभी common JavaScript types के लिए validators provide करती है -- string, number, bool, array, object, func -- साथ ही कुछ ज़्यादा advanced वाले जैसे allowed values के एक fixed set के लिए PropTypes.oneOf।

यह आपको precise होने देता है कि exactly किस shape का data एक component expect करता है।

Note: उन props के लिए PropTypes.oneOf([...]) use करें जो सिर्फ specific list of strings में से एक होने चाहिए, जैसे एक size prop।
Warning: PropTypes checks सिर्फ development mode में चलते हैं -- वे performance के लिए production builds से strip out कर दिए जाते हैं।

उदाहरण: Common PropTypes Validators

markup
// Run in your local React project (npm install required)
import PropTypes from 'prop-types';

function StatusBadge({ status }) {
  return <span className={`badge-${status}`}>{status}</span>;
}

StatusBadge.propTypes = {
  status: PropTypes.oneOf(['active', 'inactive', 'pending']).isRequired,
};

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

PropTypes vs TypeScript

PropTypes browser में app running होने के दौरान prop types check करता है, जबकि TypeScript code लिखते समय types check करता है, इसके run होने से पहले ही।

TypeScript ज़्यादा mistakes को earlier catch करता है, यही कारण है कि कई modern React projects इसे PropTypes से ज़्यादा prefer करते हैं।

Note: अगर आपका project पहले से TypeScript use करता है, आपको generally PropTypes की भी ज़रूरत नहीं -- TypeScript के types compile time पर वही need cover करते हैं।
Warning: PropTypes से TypeScript में switch करने के लिए prop declarations को एक अलग syntax में rewrite करना पड़ता है -- वे line-for-line interchangeable नहीं हैं।

उदाहरण: PropTypes vs TypeScript

markup
// Run in your local React project (npm install required)
// PropTypes (runtime check):
Greeting.propTypes = { name: PropTypes.string };

// TypeScript (compile-time check) equivalent:
type GreetingProps = { name: string };
function Greeting({ name }: GreetingProps) {
  return <p>Hello, {name}</p>;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. अलग prop-types package install और import करना भूल जाना -- यह React में built-in नहीं है।
  2. PropTypes use करना और यह उम्मीद करना कि वे app को चलने से रोकेंगी -- वे सिर्फ warnings log करती हैं, वे कुछ भी block नहीं करती।
  3. PropTypes (runtime checking) को TypeScript (compile-time checking) के साथ confuse करना -- वे एक similar problem को अलग तरीके से solve करते हैं।
चैप्टर सारांश
  • PropTypes आपको एक component को receive होने वाले हर prop की expected type declare करने देते हैं।
  • वे अलग prop-types npm package से आते हैं, React core से नहीं।
  • Mismatched prop types development में एक console warning produce करती हैं, error नहीं।
  • कई modern projects stronger type safety के लिए PropTypes के बजाय TypeScript use करते हैं।
ब्राउज़र सपोर्ट

कोई browser-specific restrictions नहीं -- prop-types किसी भी React version में काम करता है लेकिन इसे एक अलग npm package के रूप में install किया जाना चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.