Functional Components and Props
# CHAPTER 7
Functional Components and Props
1. Introduction
Imagine building an app like Instagram. The feed contains 100 posts. You are not going to write the code for a "Post Box" 100 separate times. You are going to write it once, and reuse it 100 times. But how does that generic Post Box know whether to display Alice's photo or Bob's photo? The answer is Props. In this chapter, we will master Functional Components and Props. We will learn how to make our components dynamic by passing "Properties" (data) down from Parent components to Child components, allowing us to build highly reusable, dynamic UI architectures.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the definition and strict rules of Props.
- Pass simple strings and numbers to child components.
- Access received props within a child component.
- Utilize Object Destructuring for cleaner prop extraction.
- Render dynamic UI elements based on prop data.
3. What are Props?
Props (short for Properties) are arguments passed into React components. Think of a function:function multiply(a, b). The arguments a and b dictate how the function behaves.
Props work exactly the same way. The parent component passes data, and the child component uses that data to render the UI.
The Golden Rule: Props are READ-ONLY. A child component can *never* modify the props it receives. Data flows exactly one way: Downwards.
4. Passing and Receiving Props
Let's build a reusableGreeting component.
1. The Parent (Passing Data) The parent uses custom HTML-like attributes to pass data. We can name these attributes anything we want!
2. The Child (Receiving Data)
The child receives an object called props. It contains all the data the parent passed down!
*Output on screen: "Hello, Alice!" followed by "Hello, Bob!"*
5. Passing Numbers, Booleans, and Variables
If you pass a hardcoded string, you use quotes:name="Alice".
If you pass a number, a boolean, or a variable, you MUST wrap it in curly braces {} so JSX knows it is JavaScript math/logic!
6. Destructuring Props (The Professional Way)
Writingprops.name, props.age, props.city over and over gets tedious.
Modern React developers use JavaScript Object Destructuring to extract the variables directly in the function parameters!
7. Default Props
What happens if the parent forgets to pass thename prop? The screen will show "Name: " (blank).
To prevent this, you can assign default values during destructuring!
8. Mini Project: User Profile Card App
Objective: Build a dynamic feed using reusable components.9. Common Mistakes
-
Trying to change Props: If a child component tries to execute
props.name = "New Name";, React will throw an error. Remember: Props are read-only. They are instructions from the parent. If the data needs to change dynamically over time, it must be stored in State (which we will learn next!).
10. Best Practices
- Prop Drilling Warning: If you have to pass a prop from Parent -> Child -> Grandchild -> GreatGrandchild just so the bottom component can read it, you have created "Prop Drilling." This is an architectural smell. In future chapters, we will learn how to use Context and Redux to solve this!
11. Practice Exercises
-
1.
Create a
ProductItemcomponent that destructures two props:titleandprice.
-
2.
How do you pass a boolean value of
falseto a prop namedisSoldOutfrom the parent component?
12. MCQs with Answers
When a parent component passes a variable to a child component via props, what syntax is strictly required in the parent's JSX?
Why is Object Destructuring highly recommended when writing functional components that receive props?
13. Interview Questions
- Q: Explain the concept of "One-Way Data Flow" in React. How do props enforce this paradigm?
- Q: Contrast the process of passing string literals versus passing dynamic JavaScript expressions via props. Why is the syntax different?
-
Q: Provide an example of how you would set a default value for a prop to prevent the UI from rendering
undefinedif the parent fails to pass data.
14. FAQs
Q: Can I pass a function as a prop? A: Yes! This is a fundamental pattern in React. If a child component has a button, the parent can pass a function down via props (e.g.,onDelete={handleDelete}). When the child button is clicked, it executes the parent's function!