Challenge - 5 Problems
Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when the button is pressed?
Consider this React Native component using a callback prop. What will be logged when the button is pressed?
React Native
import React from 'react'; import { Button, View } from 'react-native'; function Child({ onPress }) { return <Button title="Press me" onPress={onPress} />; } export default function Parent() { const handlePress = () => { console.log('Button pressed!'); }; return <View><Child onPress={handlePress} /></View>; }
Attempts:
2 left
💡 Hint
The Child component receives a function as a prop and uses it as the button's onPress handler.
✗ Incorrect
The Parent passes a function handlePress as the onPress prop to Child. When the button is pressed, Child calls onPress, which triggers handlePress and logs the message.
🧠 Conceptual
intermediate1:30remaining
Why use callback props in React Native?
Why do React Native components often use callback props like onPress instead of handling events internally?
Attempts:
2 left
💡 Hint
Think about who decides what happens when a button is pressed.
✗ Incorrect
Callback props let parent components decide what to do when a child component triggers an event, keeping components reusable and flexible.
📝 Syntax
advanced2:00remaining
Identify the correct callback prop usage
Which option correctly passes a callback prop to a child component and uses it on a button press?
React Native
function Child({ onAction }) {
return <Button title="Click" onPress={onAction} />;
}
function Parent() {
const doSomething = () => alert('Done');
return <Child ??? />;
}Attempts:
2 left
💡 Hint
Passing the function itself without calling it is key.
✗ Incorrect
Option D correctly passes the function doSomething as a callback prop. Option D calls the function immediately, passing its result. Option D passes a function returning doSomething, not calling it. Option D is missing JSX brackets.
❓ lifecycle
advanced1:30remaining
When is a callback prop executed?
Given a child component that calls a callback prop on a button press, when exactly is the callback function executed?
React Native
function Child({ onTap }) {
return <Button title="Tap" onPress={onTap} />;
}Attempts:
2 left
💡 Hint
Think about user interaction with the button.
✗ Incorrect
The callback prop onTap is executed only when the user presses the button, not during rendering or mounting.
🔧 Debug
expert2:30remaining
Why does this callback prop cause an error?
This Parent component passes a callback prop to Child, but pressing the button causes an error. What is the cause?
React Native
function Child({ onPress }) {
return <Button title="Go" onPress={onPress} />;
}
function Parent() {
const message = 'Hello';
return <Child onPress={message} />;
}Attempts:
2 left
💡 Hint
Check the type of the prop passed to onPress.
✗ Incorrect
The Button's onPress prop must be a function. Passing a string causes a runtime error when the button is pressed.