Callback props let a parent component talk to a child component by sending a function. This helps the child tell the parent when something happens.
Callback props in React Native
function Parent() {
function handleEvent() {
// do something
}
return <Child onEvent={handleEvent} />;
}
function Child({ onEvent }) {
return <Button onPress={onEvent} title="Click me" />;
}The parent sends a function as a prop to the child.
The child calls this function when needed, like on a button press.
function Parent() {
function sayHello() {
alert('Hello from parent!');
}
return <Child onPress={sayHello} />;
}
function Child({ onPress }) {
return <Button onPress={onPress} title="Greet" />;
}function Parent() {
function logMessage(message) {
console.log('Message:', message);
}
return <Child onSend={logMessage} />;
}
function Child({ onSend }) {
return <Button onPress={() => onSend('Hi!')} title="Send" />;
}This app shows a number and a button. When you press the button, the number goes up by one. The child button tells the parent to update the number using a callback prop.
import React from 'react'; import { View, Button, Text } from 'react-native'; export default function Parent() { const [count, setCount] = React.useState(0); function handleIncrement() { setCount(prevCount => prevCount + 1); } return ( <View style={{ padding: 20 }}> <Text>Count: {count}</Text> <Child onIncrement={handleIncrement} /> </View> ); } function Child({ onIncrement }) { return <Button title="Add 1" onPress={onIncrement} />; }
Always name callback props starting with 'on' to show they are event handlers.
Use arrow functions in the child if you need to pass arguments to the callback.
Callback props keep your components reusable and easy to manage.
Callback props let children tell parents about events.
Parents send functions as props; children call them when needed.
This pattern helps keep components simple and communication clear.
Practice
callback props in React Native components?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 CQuick Check:
Callback props = child-to-parent communication [OK]
- Thinking callback props manage styling
- Confusing callback props with state management
- Assuming callback props fetch data
onPress from a parent to a child component in React Native?Solution
Step 1: Recall JSX prop syntax
Props are passed as attributes with values inside curly braces for functions.Step 2: Match correct syntax
usesonPress={handlePress}, which is valid JSX syntax.Final Answer:
<ChildComponent onPress={handlePress} /> -> Option AQuick Check:
JSX props use = and curly braces for functions [OK]
- Using colon : instead of =
- Using arrow => inside prop
- Adding parentheses () in prop name
function Parent() {
const handleClick = () => console.log('Clicked!');
return <Child onPress={handleClick} />;
}
function Child({ onPress }) {
return <Button title="Press me" onPress={onPress} />;
}Solution
Step 1: Trace callback prop usage
The Parent passeshandleClickasonPressprop to Child.Step 2: Check Child button onPress
The Child usesonPressprop as the button's onPress handler, so pressing callshandleClick.Final Answer:
Clicked! -> Option BQuick Check:
Button press triggers parent's callback [OK]
- Assuming onPress is undefined
- Thinking button title logs
- Ignoring callback function call
function Parent() {
const handlePress = () => alert('Pressed');
return <Child onPress={handlePress()} />;
}
function Child({ onPress }) {
return <Button title="Click" onPress={onPress} />;
}Solution
Step 1: Analyze prop passing
InonPress={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 DQuick Check:
Pass function reference, not call it [OK]
- Calling function instead of passing reference
- Thinking alert is invalid in React Native
- Ignoring destructuring in Child
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 AQuick Check:
Child controls event call; parent receives updates [OK]
- Letting child update parent state directly
- Ignoring conditional logic in child
- Using global variables for communication
Ready to prove it?
Pass the test and this pattern moves unknown
