0
0
Reactframework~20 mins

Parent-child data flow in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parent-Child Data Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this React component?

Consider the following React components. What will be displayed on the screen?

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

function Child({ message }) {
  return <p>{message}</p>;
}

export default function Parent() {
  const [msg] = useState('Hello from Parent');
  return <Child message={msg} />;
}
AError: message is not defined
Bundefined
CHello from Parent
DNothing is rendered
Attempts:
2 left
💡 Hint

Look at how the message prop is passed from Parent to Child.

state_output
intermediate
1:30remaining
What is the value of count after clicking the button once?

In this React component, what will be the value of count after the button is clicked once?

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

function Child({ onClick }) {
  return <button onClick={onClick}>Click me</button>;
}

export default function Parent() {
  const [count, setCount] = useState(0);
  return <>
    <p>{count}</p>
    <Child onClick={() => setCount(count + 1)} />
  </>;
}
ANaN
B1
C0
DError: setCount is not a function
Attempts:
2 left
💡 Hint

Check how the onClick function updates the state in the Parent.

🔧 Debug
advanced
2:00remaining
Why does the Child component not update when Parent state changes?

Look at this React code. The Parent updates its state, but the Child does not show the new value. Why?

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

function Child({ value }) {
  return <p>{value}</p>;
}

export default function Parent() {
  const [count, setCount] = useState(0);
  const val = useMemo(() => count, []);
  return <>
    <button onClick={() => setCount(count + 1)}>Increment</button>
    <Child value={val} />
  </>;
}
AThe Child component does not update because the button has no onClick handler.
BThe Child component does not update because the 'value' prop is not passed correctly.
CThe Child component does not update because 'setCount' is not called.
DThe Child component does not update because the 'val' variable is not state and does not update.
Attempts:
2 left
💡 Hint

Think about what causes React components to re-render and memoized values to recompute.

📝 Syntax
advanced
1:30remaining
Which option correctly passes a callback from Parent to Child?

Which of these code snippets correctly passes a callback function from a Parent component to a Child component in React?

Afunction Parent() { const handleClick = () => alert('Clicked'); return <Child onClick={handleClick} />; }
Bfunction Parent() { const handleClick = () => alert('Clicked'); return <Child onClick={handleClick()} />; }
Cfunction Parent() { const handleClick = () => alert('Clicked'); return <Child onClick={handleClick} />(); }
Dfunction Parent() { const handleClick = () => alert('Clicked'); return <Child onClick={() => handleClick} />; }
Attempts:
2 left
💡 Hint

Remember to pass the function itself, not the result of calling it.

🧠 Conceptual
expert
2:30remaining
What is the best way to update Parent state from Child in React?

You want a Child component to update the Parent's state. Which approach follows React's recommended pattern?

APass a state setter function from Parent to Child as a prop, then call it inside Child.
BHave Child directly modify Parent's state variable using a global variable.
CUse React's Context API to let Child update Parent's state without props.
DUse a ref in Child to access and change Parent's state variable directly.
Attempts:
2 left
💡 Hint

Think about how React encourages data flow and state management.