Passing Data with Props
Define a reusable component that accepts props, pass values from a parent component, and use PropTypes or TypeScript to document expected prop shapes.
What Are Props?
Props (short for properties) are the mechanism React components use to receive input from their parent. A parent component renders a child component and passes data to it through attributes in JSX — just like HTML attributes, but for any JavaScript value including strings, numbers, objects, arrays, and functions. The child component receives all its props as a single object argument. Props flow in one direction: from parent down to child. A child component can never modify its own props — they are read-only.
import { Text, View } from 'react-native';
// Child component receives props
function Greeting({ name, age }) {
return (
<View>
<Text>Hello, {name}!</Text>
<Text>You are {age} years old.</Text>
</View>
);
}
// Parent passes data through props
export default function App() {
return (
<View style={{ flex: 1, padding: 24 }}>
<Greeting name='Alice' age={30} />
<Greeting name='Bob' age={25} />
</View>
);
}Destructuring Props
You can access props in two ways: as a single props object (function Card(props) { props.title }) or by destructuring directly in the function signature (function Card({ title, subtitle })). Destructuring is the preferred style because it makes the accepted props explicit and the code more concise. You can also provide default values for props in the destructuring syntax: { size = 16, color = '#333' }. This means the prop is optional and falls back to the default when not provided by the parent.
import { Text, View, StyleSheet } from 'react-native';
// Destructuring with default values
function Badge({ label, color = '#4f86f7', size = 14 }) {
return (
<View style={[styles.badge, { backgroundColor: color }]}>
<Text style={[styles.text, { fontSize: size }]}>{label}</Text>
</View>
);
}
export default function App() {
return (
<View style={{ gap: 8, padding: 16 }}>
<Badge label='New' /> {/* uses defaults */}
<Badge label='Sale' color='#e74c3c' /> {/* custom color */}
<Badge label='Pro' color='#2ecc71' size={12} />
</View>
);
}
const styles = StyleSheet.create({
badge: { paddingHorizontal: 10, paddingVertical: 4, borderRadius: 12, alignSelf: 'flex-start' },
text: { color: '#fff', fontWeight: 'bold' },
});All lessons in this course
- Passing Data with Props
- Managing Component State with useState
- Lifting State Up
- Building an Interactive Counter App