Grokking React Native
0% completed
Passing Functions as Props
A profile screen shows a follower count, and the button that changes the count lives in a child component. The count belongs to the parent, and props only travel downward, so the child has no way to send a new value up. The solution uses something you already know: a function is a value, so a parent can pass one as a prop. When the child calls it, the code that runs belongs to the parent.
Passing a Function
Start with the smallest case. The parent defines a function and passes it down, and the child gives it to its Button.
import { View, Button, Alert } from 'react-native';
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%