Bird
Raised Fist0
Reactframework~5 mins

What are props in React

Choose your learning style10 modes available
Introduction

Props let you pass information from one component to another in React. They help components share data and work together.

You want to show a user's name inside a greeting component.
You need to customize a button's label and color from outside the button component.
You want to reuse a card component but show different content each time.
You want to send a number to a counter component to start counting from that number.
Syntax
React
function ComponentName(props) {
  return <div>{props.someValue}</div>;
}

// Usage
<ComponentName someValue="Hello" />
Props are read-only inside the component receiving them.
Props are passed like HTML attributes when using a component.
Examples
This shows a greeting with the name passed as a prop.
React
function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}

<Welcome name="Alice" />
The button's label and color come from props.
React
function Button(props) {
  return <button style={{ backgroundColor: props.color }}>{props.label}</button>;
}

<Button label="Click me" color="blue" />
This uses destructuring to get props directly.
React
function Message({ text }) {
  return <p>{text}</p>;
}

<Message text="Welcome to React!" />
Sample Program

This app shows two greetings with different names passed as props.

React
import React from 'react';

function Greeting(props) {
  return <h2>Hello, {props.user}!</h2>;
}

export default function App() {
  return (
    <div>
      <Greeting user="Sam" />
      <Greeting user="Lee" />
    </div>
  );
}
OutputSuccess
Important Notes

Props cannot be changed inside the component that receives them.

Use props to make components reusable and flexible.

Summary

Props pass data from parent to child components.

They are like function arguments for components.

Props help components show different content without rewriting code.

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