Bird
Raised Fist0
Reactframework~15 mins

What are props in React - Hands-On Activity

Choose your learning style10 modes available
What are props
📖 Scenario: You want to create a simple React component that shows a greeting message. The message should come from outside the component so you can reuse it with different greetings.
🎯 Goal: Build a React component that receives a greeting message as a prop and displays it inside a paragraph.
📋 What You'll Learn
Create a functional React component named Greeting
Use a prop named message to pass the greeting text
Render the message inside a <p> element
Use the Greeting component inside App and pass the message 'Hello, friend!'
💡 Why This Matters
🌍 Real World
Props let you build flexible UI parts that change based on data, like showing user names or messages.
💼 Career
Understanding props is essential for React developers to create reusable and maintainable components.
Progress0 / 4 steps
1
Create the Greeting component
Create a React functional component called Greeting that accepts props as a parameter.
React
Hint

Start by writing a function named Greeting that takes props as input.

2
Add a message prop
Inside the Greeting component, use props.message to access the greeting text.
React
Hint

Use curly braces {} inside JSX to show the value of props.message.

3
Use Greeting in App
Create a React functional component called App that returns the Greeting component with the message prop set to 'Hello, friend!'.
React
Hint

Return the Greeting component with the message prop inside App.

4
Export App component
Add export default App; at the end of the code to make App the main component.
React
Hint

This line makes App the component React shows on the page.

Practice

(1/5)
1. What are props in React?
easy
A. Data passed from a parent component to a child component
B. A way to store component state internally
C. Functions that update the component's state
D. A method to style components

Solution

  1. Step 1: Understand the role of props

    Props are used to send data from a parent component down to a child component in React.
  2. Step 2: Differentiate props from state and methods

    Props are not for storing state or styling; they are inputs like function arguments.
  3. Final Answer:

    Data passed from a parent component to a child component -> Option A
  4. Quick Check:

    Props = Parent to child data [OK]
Hint: Props are like function arguments passed down [OK]
Common Mistakes:
  • Confusing props with state
  • Thinking props change inside the child
  • Believing props are used for styling
2. Which of the following is the correct way to pass a prop named title with value "Hello" to a component Greeting?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recognize JSX prop syntax

    Props are passed as attributes with key-value pairs inside JSX tags, using quotes for strings.
  2. Step 2: Check each option's syntax

    correctly uses title="Hello". misses the key, C uses unquoted value, D misses quotes.
  3. Final Answer:

    <Greeting title="Hello" /> -> Option D
  4. Quick Check:

    Props syntax = key="value" [OK]
Hint: Props look like HTML attributes with quotes [OK]
Common Mistakes:
  • Omitting the prop name
  • Not quoting string values
  • Using curly braces without variables
3. Given this component:
function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Usage:
<Welcome name="Alice" />

What will be rendered on the screen?
medium
A. Hello, props.name!
B. Hello, !
C. Hello, Alice!
D. Error: props is undefined

Solution

  1. Step 1: Identify how props are used in the component

    The component uses props.name inside JSX to display the passed name.
  2. Step 2: Check the usage of the component

    The component is called with name="Alice", so props.name equals "Alice".
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    props.name = Alice [OK]
Hint: Props values appear where you use {props.key} [OK]
Common Mistakes:
  • Printing the literal string 'props.name'
  • Forgetting to pass the prop
  • Assuming props is undefined
4. What is wrong with this React component usage?
function Message(props) {
  return <p>{props.text}</p>;
}

// Usage:
<Message text=Hello />
medium
A. The function should return a string, not JSX
B. The prop value Hello should be in quotes
C. Props cannot be used inside <p> tags
D. The component name should be lowercase

Solution

  1. Step 1: Check the prop syntax in usage

    The prop text=Hello is missing quotes around the string value.
  2. Step 2: Recall JSX rules for string props

    String props must be quoted like text="Hello" or wrapped in curly braces if a variable.
  3. Final Answer:

    The prop value Hello should be in quotes -> Option B
  4. Quick Check:

    String props need quotes [OK]
Hint: Always quote string props in JSX [OK]
Common Mistakes:
  • Leaving string props unquoted
  • Using lowercase component names
  • Misunderstanding JSX return types
5. You want to create a reusable Button component that shows different labels based on props. Which approach correctly uses props to achieve this?
function Button(props) {
  return <button>{props.label}</button>;
}

// Usage:
<Button label="Save" />
<Button label="Cancel" />
hard
A. This approach works because props.label changes the button text dynamically
B. This approach is wrong because props cannot be used inside button tags
C. You must use state instead of props to change button labels
D. Button labels must be hardcoded inside the component

Solution

  1. Step 1: Understand props usage for dynamic content

    Props allow passing different values to the same component to show varied content.
  2. Step 2: Verify the example usage

    The Button component uses props.label inside the button, and different labels are passed in usage.
  3. Final Answer:

    This approach works because props.label changes the button text dynamically -> Option A
  4. Quick Check:

    Props enable reusable components [OK]
Hint: Use props to customize component content easily [OK]
Common Mistakes:
  • Thinking props can't change displayed text
  • Confusing props with state for static labels
  • Hardcoding values inside components
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard