Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Basics – React + Next.js की नींव  ·  Lesson 05

React में Props

Props क्या हैं?

Props (properties का छोटा रूप) से आप component के अंदर data भेजते हैं — बिल्कुल HTML tag के attributes की तरह। एक ही component अलग-अलग props मिलने पर अलग content दिखा सकता है।

Parent<Greeting name="Riya" />props object{ name: "Riya" }ChildGreeting(props)OutputHello, Riya!
Props parent से child तक जाते हैं

Props भेजना और पढ़ना

JSX
function Greeting(props) {
  return <p>Hello, {props.name}! You are in class {props.cls}.</p>
}

export default function Page() {
  return (
    <>
      <Greeting name="Riya" cls={10} />
      <Greeting name="Aman" cls={12} />
    </>
  )
}
Hello, Riya! You are in class 10. Hello, Aman! You are in class 12.

React सारे attributes को props नाम के एक object में इकट्ठा करके function को पहले argument के रूप में देता है।

Destructuring और default values

हर जगह props.something लिखना लंबा हो जाता है। ज़्यादातर developers props को destructure करते हैं और optional वालों को default value देते हैं:

JSX
// Destructure props directly in the function brackets
function Price({ amount, currency = '₹', discount = 0 }) {
  const final = amount - (amount * discount) / 100
  return (
    <p>
      {currency}{final.toLocaleString('en-IN')}
      {discount > 0 && <small> ({discount}% off)</small>}
    </p>
  )
}

<Price amount={4999} />               // ₹4,999
<Price amount={4999} discount={20} /> // ₹3,999.2 (20% off)

कोई भी value prop बन सकती है

JSX
function Course({ title, fee, topics, isNew, onEnquire, teacher }) {
  return (
    <div>
      <h3>{title} {isNew && '🆕'}</h3>
      <p>Fee: ₹{fee} · Teacher: {teacher.name}</p>
      <ul>{topics.map((t) => <li key={t}>{t}</li>)}</ul>
      <button onClick={onEnquire}>Enquire</button>
    </div>
  )
}

<Course
  title="Python + AI"              // string
  fee={3499}                       // number
  isNew                            // boolean true
  topics={['Basics', 'Pandas']}    // array
  teacher={{ name: 'Gagan Sir' }}  // object
  onEnquire={() => alert('Thanks!')} // function (Client Component only)
/>
💡 Text के लिए quotes; बाकी सब (numbers, booleans, arrays, objects, functions) के लिए { }। Object के लिए double braces देखिए: बाहर वाले = JavaScript, अंदर वाले = object।

children prop

Component के opening और closing tag के बीच जो भी रखते हैं, वह children बनकर आता है। Layouts और wrappers ऐसे ही काम करते हैं — Next.js का layout.jsx भी page को children के रूप में पाता है।

JSX – components/Box.jsx
// components/Box.jsx — a wrapper that accepts any content
export default function Box({ title, children }) {
  return (
    <section style={{ border: '1px solid #e2e8f0', borderRadius: 12, padding: 16 }}>
      <h2>{title}</h2>
      {children}
    </section>
  )
}

// usage — everything between <Box> and </Box> is "children"
<Box title="Admissions open">
  <p>New batch starts on 1 November.</p>
  <a href="/contact">Book a demo</a>
</Box>

One-way data flow

HomePagecourses array इसी के पास← data का मालिकCourseCardएक course prop के रूप में← read-onlyPrice / Badgefee / level मिलते हैं← read-only
Data tree में नीचे जाता है; children parent का data सीधे नहीं बदलते

Props सिर्फ़ नीचे जाते हैं। Child parent का data edit नहीं कर सकता। अगर child को parent को कुछ बताना है (जैसे "button click हुआ"), तो parent एक function prop के रूप में भेजता है और child उसे call करता है।

असली example: EduSite का CourseCard

JSX – components/CourseCard.jsx
// components/CourseCard.jsx (from the EduSite project)
import Image from 'next/image'
import Link from 'next/link'
import { rupees } from '@/lib/courses'

export default function CourseCard({ course }) {
  return (
    <article className="card">
      <Link href={`/courses/${course.slug}`}>
        <Image src={course.image} alt={course.title} width={600} height={338} />
      </Link>
      <div style={{ padding: 18 }}>
        <span className="badge">{course.level}</span>
        <h3>{course.title}</h3>
        <p>{course.summary}</p>
        <p><b>{rupees(course.fee)}</b> · {course.duration}</p>
      </div>
    </article>
  )
}

// app/page.jsx
{courses.slice(0, 3).map((c) => <CourseCard key={c.slug} course={c} />)}
Props से बने course cards
हर card एक ही component है, बस props अलग हैं

आम गलतियाँ

  • fee="2499" string भेजता है; fee * 2 जैसी maths चल भी जाए पर comparisons बिगड़ते हैं। fee={2499} लिखें।
  • Child के अंदर prop बदलना (props.name = 'x') — allowed नहीं; state use करें।
  • Object के चारों ओर braces भूलना: style={color:'red'} → style={{ color: 'red' }} होना चाहिए।
  • Server Component से Client Component को onClick function भेजना — handler को Client Component के अंदर ले जाएँ।

सारांश

  • Props parent से child को HTML attributes की तरह data भेजते हैं।
  • उन्हें destructure करें और defaults दें: function X({ a, b = 1 })।
  • children tags के बीच का content है।
  • Props read-only हैं और एक दिशा में बहते हैं: नीचे।
✅ Practice

name, roll, marks (array) props वाला StudentCard component बनाइए। Total दिखाइए और total 270 से ज़्यादा हो तो "Topper ⭐" दिखाइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या component अपने props खुद बदल सकता है?
नहीं। Props read-only होते हैं। अगर value समय के साथ बदलनी है, तो उसे parent में state (useState) में रखें और नई value prop के रूप में नीचे भेजें।
Props और state में क्या अंतर है?
Props parent से आते हैं और child उन्हें बदल नहीं सकता। State component की अपनी होती है और update हो सकती है, जिससे component re-render होता है।
Number या boolean prop कैसे भेजें?
Curly braces से: fee={2499} या isNew={true}। Text quotes में लिख सकते हैं: title="Python"। सिर्फ़ isNew लिखने का मतलब भी true है।
क्या Server Component से Client Component को props दे सकते हैं?
हाँ, अगर values serialise हो सकें (text, numbers, arrays, plain objects)। Server Component से Client Component को functions नहीं भेज सकते, Server Actions को छोड़कर।
← Next.js Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicuseState और Events आपकी प्रगति: 5 / 37 (14%) · Lesson 6 / 37 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें