0
0
React Nativemobile~20 mins

Callback props in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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>;
}
ANothing happens because onPress is not called.
BThe message 'Button pressed!' is logged to the console.
CAn error occurs because onPress is not a function.
DThe app crashes due to missing onPress prop.
Attempts:
2 left
💡 Hint
The Child component receives a function as a prop and uses it as the button's onPress handler.
🧠 Conceptual
intermediate
1:30remaining
Why use callback props in React Native?
Why do React Native components often use callback props like onPress instead of handling events internally?
ATo make components slower but more complex.
BBecause React Native does not support internal event handling.
CTo allow parent components to control behavior when events happen in child components.
DTo prevent any interaction with the UI.
Attempts:
2 left
💡 Hint
Think about who decides what happens when a button is pressed.
📝 Syntax
advanced
2: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 ??? />;
}
A<Child onAction=doSomething />
B<Child onAction={doSomething()} />
C<Child onAction={() => doSomething} />
D<Child onAction={doSomething} />
Attempts:
2 left
💡 Hint
Passing the function itself without calling it is key.
lifecycle
advanced
1: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} />;
}
AOnly when the button inside Child is pressed by the user.
BWhen the Parent component mounts.
CImmediately when the Child component renders.
DWhen the Child component unmounts.
Attempts:
2 left
💡 Hint
Think about user interaction with the button.
🔧 Debug
expert
2: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} />;
}
AonPress expects a function but received a string, causing a runtime error.
BThe Child component is missing the onPress prop.
CThe Button component does not support onPress.
DThe Parent component forgot to import React.
Attempts:
2 left
💡 Hint
Check the type of the prop passed to onPress.