A callback prop is a function passed from a parent component to a child component. The child calls this function to send data or notify the parent about events.
Click to reveal answer
beginner
Why do we use callback props in React Native?
We use callback props to let child components communicate with their parents, especially to send data or trigger actions in the parent.
Click to reveal answer
beginner
How do you define a callback prop in a parent component?
You define a function in the parent and pass it as a prop to the child component, like <Child onPress={handlePress} />.
Click to reveal answer
beginner
How does a child component use a callback prop?
The child calls the function received as a prop, for example props.onPress(), to notify or send data back to the parent.
Click to reveal answer
intermediate
What happens if a callback prop is not passed to a child component?
If the child tries to call the callback but it is not passed, it can cause an error. It's good to check if the prop exists before calling it.
Click to reveal answer
What is the main purpose of a callback prop in React Native?
ATo allow child components to send data to parent components
BTo style components dynamically
CTo fetch data from the internet
DTo create new components
✗ Incorrect
Callback props let child components communicate with parents by sending data or events.
How do you pass a callback prop from a parent to a child component?
A<Child style={functionName} />
B<Child data={functionName} />
C<Child callback={functionName} />
D<Child onClick={functionName} />
✗ Incorrect
You pass the function as a prop, often named to describe the event, like callback or onPress.
In the child component, how do you call the callback prop named onPress?
Athis.onPress()
Bprops.onPress()
Ccall.onPress()
DonPress()
✗ Incorrect
The child calls the function from props using props.onPress() to notify the parent.
What should you do before calling a callback prop in a child component?
ADelete the callback prop
BChange the callback prop
CIgnore the callback prop
DCheck if the callback prop exists
✗ Incorrect
Checking if the callback exists prevents errors if the parent did not pass it.
Which of these is NOT a reason to use callback props?
ATo pass styling information
BTo send data from child to parent
CTo notify parent of user actions
DTo update parent state from child
✗ Incorrect
Styling is usually passed as style props, not callback functions.
Explain how callback props enable communication between parent and child components in React Native.
Think about how a child tells the parent something happened.
You got /3 concepts.
Describe the steps to safely use a callback prop in a child component.
Avoid errors by verifying the callback before calling.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using callback props in React Native components?
easy
A. To manage component state internally
B. To style components dynamically
C. To allow child components to communicate events back to parent components
D. To fetch data from an API
Solution
Step 1: Understand callback props role
Callback props are functions passed from parent to child to notify the parent about events.
Step 2: Identify communication direction
They let children send information or events back to parents, enabling communication.
Final Answer:
To allow child components to communicate events back to parent components -> Option C
Quick Check:
Callback props = child-to-parent communication [OK]
Hint: Callback props let children tell parents about events [OK]
Common Mistakes:
Thinking callback props manage styling
Confusing callback props with state management
Assuming callback props fetch data
2. Which of the following is the correct way to pass a callback prop named onPress from a parent to a child component in React Native?
easy
A.
B.
C. handlePress />
D.
Solution
Step 1: Recall JSX prop syntax
Props are passed as attributes with values inside curly braces for functions.
Step 2: Match correct syntax
uses onPress={handlePress}, which is valid JSX syntax.
Final Answer:
<ChildComponent onPress={handlePress} /> -> Option A
Quick Check:
JSX props use = and curly braces for functions [OK]
Hint: Use = and curly braces to pass functions as props [OK]
Common Mistakes:
Using colon : instead of =
Using arrow => inside prop
Adding parentheses () in prop name
3. Given the code below, what will be logged when the button is pressed?
function Parent() {
const handleClick = () => console.log('Clicked!');
return <Child onPress={handleClick} />;
}
function Child({ onPress }) {
return <Button title="Press me" onPress={onPress} />;
}
medium
A. Nothing happens
B. Clicked!
C. Error: onPress is not a function
D. Button title is logged
Solution
Step 1: Trace callback prop usage
The Parent passes handleClick as onPress prop to Child.
Step 2: Check Child button onPress
The Child uses onPress prop as the button's onPress handler, so pressing calls handleClick.
Final Answer:
Clicked! -> Option B
Quick Check:
Button press triggers parent's callback [OK]
Hint: Child calls parent's function on event [OK]
Common Mistakes:
Assuming onPress is undefined
Thinking button title logs
Ignoring callback function call
4. Identify the error in this React Native code snippet:
D. handlePress is called immediately instead of passed as a function
Solution
Step 1: Analyze prop passing
In onPress={handlePress()}, the function is called immediately, passing its result (undefined) instead of the function.
Step 2: Understand correct usage
We should pass the function itself without calling it: onPress={handlePress}.
Final Answer:
handlePress is called immediately instead of passed as a function -> Option D
Quick Check:
Pass function reference, not call it [OK]
Hint: Pass function, don't call it when passing as prop [OK]
Common Mistakes:
Calling function instead of passing reference
Thinking alert is invalid in React Native
Ignoring destructuring in Child
5. You want a child component to notify the parent when a text input changes, but only if the input length is more than 3 characters. How should you implement the callback prop pattern to achieve this?
hard
A. Pass a callback prop from parent; child calls it only when input length > 3
B. Child manages input state and updates parent state directly
C. Parent listens to child's internal state without callback
D. Use a global variable to share input value between components
Solution
Step 1: Understand callback prop usage for conditional events
The parent passes a function as a prop to the child to receive input changes.
Step 2: Implement conditional call in child
The child calls the callback only if the input length is more than 3, controlling when to notify the parent.
Final Answer:
Pass a callback prop from parent; child calls it only when input length > 3 -> Option A