0
0
Reactframework~20 mins

Component composition in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Component Composition 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 nested components?

Consider these React components composed together. What will be rendered inside the <App /> component?

React
function Greeting({ name }) {
  return <p>Hello, {name}!</p>;
}

function Wrapper({ children }) {
  return <div className="wrapper">{children}</div>;
}

function App() {
  return (
    <Wrapper>
      <Greeting name="Alice" />
    </Wrapper>
  );
}
A<p>Hello, Alice!</p>
B<div class="wrapper"><p>Hello, Alice!</p></div>
C<div><p>Hello, Alice!</p></div>
D<div class="wrapper">Hello, Alice!</div>
Attempts:
2 left
💡 Hint

Remember that children props render the nested components inside the parent.

state_output
intermediate
2:00remaining
How does state flow in composed components?

Given these components, what is the displayed count after clicking the button once?

React
import React, { useState } from 'react';

function Counter({ count }) {
  return <p>Count: {count}</p>;
}

function Button({ onClick }) {
  return <button onClick={onClick}>Increment</button>;
}

function App() {
  const [count, setCount] = useState(0);
  return (
    <>
      <Counter count={count} />
      <Button onClick={() => setCount(count + 1)} />
    </>
  );
}
ACount: 1
BCount: 0
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint

Clicking the button triggers setCount to update the state.

📝 Syntax
advanced
2:00remaining
Which option correctly composes components with props forwarding?

Which code correctly forwards all props from Wrapper to Inner component?

React
function Inner(props) {
  return <div>{props.message}</div>;
}

function Wrapper(props) {
  // Forward props to Inner
  return ???
}
Areturn <Inner props={props} />;
Breturn <Inner {...props.message} />;
Creturn <Inner message={props.message} />;
Dreturn <Inner {...props} />;
Attempts:
2 left
💡 Hint

Use spread syntax to forward all props.

🔧 Debug
advanced
2:00remaining
Why does this composed component cause an error?

What error does this code cause and why?

React
function Parent() {
  return <Child />;
}

function Child(props) {
  return <div>{props.text}</div>;
}
ASyntaxError: Unexpected token
BTypeError: Child is not a function
CReferenceError: props is not defined
DNo error, renders undefined
Attempts:
2 left
💡 Hint

Check if props is declared in the function parameters.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of component composition in React?

Choose the best explanation for why React encourages component composition.

AIt allows building complex UIs by combining small, reusable components.
BIt forces all components to share the same state object.
CIt prevents components from receiving props from parents.
DIt requires using class components instead of functions.
Attempts:
2 left
💡 Hint

Think about how small pieces can build bigger things.