الخصائص والتواصل بين المكونات
أتقن تمرير البيانات بين المكونات الأب والمكونات الابنة باستخدام الخصائص لإنشاء واجهة مستخدم مرنة.
الخصائص والتواصل بين المكونات درس مجاني في Next.js 15 Fullstack Web Apps على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Next.js 15 Fullstack Web Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Next.js 15 Fullstack Web Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Intro to Component Props
In React, props (short for properties) are how you pass data from a parent component down to a child component.
Think of them like arguments to a function. They allow you to make your components reusable and dynamic, displaying different information based on the data they receive.
Unidirectional Data Flow
One of the core principles of React is unidirectional data flow. This means data always moves in one direction: from parent to child.
- Parent components own and manage data.
- They pass this data down to their children using props.
- Child components receive props and render content based on them.
- Children cannot directly modify the props they receive; props are read-only.
Your First Prop
Let's see how to pass a simple string prop. We'll create a Greeting component that receives a name prop.
Notice how App (parent) passes name="Alice" to Greeting (child).
import React from 'react';
import ReactDOM from 'react-dom/client';
// Child Component
function Greeting(props) {
// Access the 'name' prop using props.name
return <p>Hello, {props.name}!</p>;
}
// Parent Component
function App() {
return (
<div>
<Greeting name="Alice" />
<Greeting name="Bob" />
</div>
);
}
// Render the App component
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Passing Different Data Types
Props aren't just for strings! You can pass numbers, booleans, arrays, and objects. When passing non-string values, wrap them in curly braces {}.
This example shows a ProductCard receiving various data types.
import React from 'react';
import ReactDOM from 'react-dom/client';
function ProductCard(props) {
return (
<div style={{ border: '1px solid gray', padding: '10px', margin: '10px' }}>
<h3>{props.productName}</h3>
<p>Price: ${props.price.toFixed(2)}</p>
<p>In Stock: {props.inStock ? 'Yes' : 'No'}</p>
<p>Features:</p>
<ul>
{props.features.map((feature, index) => (
<li key={index}>{feature}</li>
))}
</ul>
</div>
);
}
function App() {
const laptop = {
name: "Super Laptop",
price: 1200.50,
available: true,
specs: ["Fast CPU", "16GB RAM", "512GB SSD"]
};
return (
<div>
<ProductCard
productName={laptop.name}
price={laptop.price}
inStock={laptop.available}
features={laptop.specs}
/>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Destructuring Props for Clarity
Accessing props like props.name can get repetitive. A cleaner way is to use object destructuring directly in your component's function signature.
This makes your code more readable by explicitly showing which props a component expects.
import React from 'react';
import ReactDOM from 'react-dom/client';
// Before: function Greeting(props) { return <p>Hello, {props.name}!</p>; }
// After destructuring:
function Greeting({ name }) {
return <p>Hello, {name}!</p>;
}
function App() {
return (
<div>
<Greeting name="Charlie" />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);The Special 'children' Prop
React has a special prop called children. This prop automatically contains whatever content you pass between a component's opening and closing tags.
It's perfect for creating wrapper components like cards, layouts, or modals.
import React from 'react';
import ReactDOM from 'react-dom/client';
function Card({ title, children }) {
return (
<div style={{ border: '1px solid #ddd', padding: '15px', margin: '10px', borderRadius: '8px' }}>
<h4>{title}</h4>
<div style={{ marginTop: '10px' }}>
{children}
</div>
</div>
);
}
function App() {
return (
<div>
<Card title="User Profile">
<p>Name: <b>Jane Doe</b></p>
<p>Email: jane@example.com</p>
<button>Edit Profile</button>
</Card>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Child to Parent: Callback Props
Since data flows down, how does a child component tell its parent something happened (e.g., a button was clicked)? You pass a function as a prop!
The child calls this function, and the parent can then react to the event. This is often called a 'callback prop'.
import React from 'react';
import ReactDOM from 'react-dom/client';
// Child Component
function MyButton({ onClick, label }) {
return (
<button onClick={onClick}>
{label}
</button>
);
}
// Parent Component
function App() {
const handleButtonClick = () => {
alert("Button clicked inside the child component!");
};
return (
<div>
<p>Parent says: Click the button below!</p>
<MyButton onClick={handleButtonClick} label="Trigger Parent" />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);When Props Aren't Enough
While props are powerful, passing them through many layers of nested components can become cumbersome. This is known as prop drilling.
If you find yourself passing the same prop through 3+ components just to reach a deeply nested child, it might be a sign to consider other patterns like React Context API or state management libraries (which we'll cover in future lessons!).
Prop Challenge
Which of the following statements about React props are true?
Recap: Props, Your UI's Data
You've mastered props, a fundamental concept in React!
- Props pass data from parent to child components.
- Data flow is unidirectional; children cannot modify props.
- You can pass various data types (strings, numbers, objects, functions).
- Destructuring props makes your code cleaner.
- The
childrenprop allows flexible content embedding. - Callback props enable child-to-parent communication.
Props are the backbone of building dynamic and reusable React components. Keep practicing, and you'll be building complex UIs in no time!
تعلم TypeScript مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «الخصائص والتواصل بين المكونات» مجاني؟
نعم — نص درس «الخصائص والتواصل بين المكونات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Next.js 15 Fullstack Web Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Next.js 15 Fullstack Web Apps 4 دروس في المجموع.
ماذا ستتعلم في «الخصائص والتواصل بين المكونات»؟
أتقن تمرير البيانات بين المكونات الأب والمكونات الابنة باستخدام الخصائص لإنشاء واجهة مستخدم مرنة. تتمرن على Next.js 15 Fullstack Web Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Next.js 15 Fullstack Web Apps؟
لا تُشترط خبرة سابقة. Next.js 15 Fullstack Web Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «الخصائص والتواصل بين المكونات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Next.js 15 Fullstack Web Apps هذا؟
نعم. كل درس في Next.js 15 Fullstack Web Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مكونات React وJSX
- إدارة الحالة باستخدام Hooks
- الخصائص والتواصل بين المكونات
- تصيير القوائم والمفاتيح وواجهة المستخدم الشرطية