0
0
Reactframework~20 mins

Embedding expressions in JSX in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSX Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component?
Consider the following React component. What will it render inside the <div>?
React
import React from 'react';

function Greeting() {
  const name = 'Alice';
  const age = 25;
  return <div>Hello, {name}! You are {age} years old.</div>;
}

export default Greeting;
AHello, {name}! You are {age} years old.
BHello, Alice! You are 25 years old.
CHello, Alice! You are {age} years old.
DHello, {name}! You are 25 years old.
Attempts:
2 left
💡 Hint
Remember that expressions inside curly braces in JSX are evaluated and replaced with their values.
📝 Syntax
intermediate
1:30remaining
Which JSX expression is valid to embed a JavaScript expression?
Which of the following JSX snippets correctly embeds a JavaScript expression to show the sum of 2 + 3?
A<div>{2 + '3'}</div>
B<div>2 + 3</div>
C<div>{'2 + 3'}</div>
D<div>{2 + 3}</div>
Attempts:
2 left
💡 Hint
JSX expressions inside curly braces are evaluated as JavaScript.
state_output
advanced
2:00remaining
What will be displayed after clicking the button once?
Given this React component, what text will appear inside the <div> after clicking the button once?
React
import React, { useState } from 'react';

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

export default Counter;
ACount: 1
BCount: 0
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint
Clicking the button calls setCount with count + 1, updating the state.
🔧 Debug
advanced
2:00remaining
Why does this JSX code cause an error?
Examine this JSX snippet. Why does it cause a syntax error?
React
const element = <div>{if (true) { 'Yes' } else { 'No' }}</div>;
AJSX expressions cannot contain statements like if; only expressions are allowed.
BThe curly braces are missing around the if statement.
CThe strings 'Yes' and 'No' must be wrapped in parentheses.
DJSX requires a semicolon after the if statement inside curly braces.
Attempts:
2 left
💡 Hint
JSX expressions expect a single JavaScript expression, not statements.
🧠 Conceptual
expert
2:30remaining
What is the value of 'result' after rendering this React component?
Consider this React component. What is the value of the variable 'result' after the component renders?
React
import React from 'react';

function Example() {
  const a = 5;
  const b = 10;
  const result = <>{a > b ? 'Greater' : a < b ? 'Smaller' : 'Equal'}</>;
  return <div>{result}</div>;
}

export default Example;
AGreater
BEqual
CSmaller
Dundefined
Attempts:
2 left
💡 Hint
The expression uses nested ternary operators to compare a and b.