0
0
Reactframework~20 mins

Using props in JSX in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Props Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this React component render?
Consider this React component that uses props to display a greeting message. What will be the output when rendered with ?
React
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}
A<h1>Hello, props.name!</h1>
B<h1>Hello, {name}!</h1>
C<h1>Hello, !</h1>
D<h1>Hello, Alice!</h1>
Attempts:
2 left
💡 Hint
Remember that props are accessed as properties of the props object inside JSX using curly braces.
state_output
intermediate
2:00remaining
What is the output after clicking the button once?
This React component uses props and state. What will be the displayed text after clicking the button once?
React
import React, { useState } from 'react';

function Counter({ start }) {
  const [count, setCount] = useState(start);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

// Rendered as <Counter start={5} />
ACount: 1
BCount: 6
CCount: 5
DCount: 0
Attempts:
2 left
💡 Hint
The initial state is set from the start prop. Clicking the button increases count by 1.
📝 Syntax
advanced
2:00remaining
Which option correctly passes multiple props to a component?
You want to pass two props, title and subtitle, to a React component called Header. Which JSX syntax is correct?
A<Header title="Welcome" subtitle="Home Page" />
B<Header title="Welcome", subtitle="Home Page" />
C<Header title={"Welcome"} subtitle={Home Page} />
D<Header title='Welcome' subtitle='Home Page'>
Attempts:
2 left
💡 Hint
Props in JSX are passed like HTML attributes without commas and with quotes or curly braces.
🔧 Debug
advanced
2:00remaining
Why does this component fail to display the prop value?
This React component tries to display a prop but shows nothing. What is the cause?
React
function ShowMessage(props) {
  return <p>Message: props.text</p>;
}

// Rendered as <ShowMessage text="Hello!" />
AThe prop name 'text' is incorrect; it should be 'message'.
BThe component is missing a return statement.
CThe prop is not inside curly braces, so it is treated as plain text.
DThe component must use state to display props.
Attempts:
2 left
💡 Hint
In JSX, dynamic values must be inside curly braces to be evaluated.
🧠 Conceptual
expert
2:00remaining
What happens if you modify props inside a React component?
Consider this code inside a React functional component: props.name = "Changed"; What will happen when you try to modify props like this?
AReact will throw an error because props are read-only and cannot be changed.
BThe component will update and re-render with the new prop value.
CThe prop value changes only inside the component but does not affect the parent.
DModifying props will cause the component to enter an infinite render loop.
Attempts:
2 left
💡 Hint
Props are meant to be read-only inputs to components.