This tutorial aims to shed light on the fundamental building blocks of React Native applications, namely Components and Props. It will guide you through the process of creating components and passing data using props.
By the end of this tutorial, you'll be able to:
- Understand the concepts of Components and Props in React Native
- Create and use Components
- Pass and use Props to transfer data between Components
A basic understanding of JavaScript and React Native is required. Familiarity with ES6 syntax (especially arrow functions) will be helpful.
Components are the building blocks of any React Native application. A component is a JavaScript function or class that optionally accepts inputs i.e., props and returns a React element that describes how a section of the UI should appear.
Props (short for properties) are how components pass and receive data from each other in React Native. A parent component can pass data to a child component via props.
A component can be created as a function or as a class. Here's a basic example of a functional component:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
The above component, when called with <Welcome name="John"/>
, will return "Hello, John" in a header tag.
Props are passed to components in a way similar to HTML tag attributes. Here's how you can pass a prop to the Welcome
component:
<Welcome name="John" />
Let's see a practical example of using components and props in a React Native application.
// Importing necessary modules
import React from 'react';
import { Text, View } from 'react-native';
// Creating a functional component
function Greeting(props) {
return (
<View>
<Text>Hello {props.name}!</Text>
</View>
);
}
// Using the Greeting component
export default function App() {
return (
<View>
<Greeting name='John' />
<Greeting name='Jane' />
</View>
);
}
In the above code:
- We first import the necessary modules
- We then create a functional component called Greeting
that accepts props
as an argument
- Inside the Greeting
component, we return a Text
component that displays a greeting message with the name passed as a prop
- In the App
component, we use the Greeting
component twice, each time with a different name passed as a prop
The expected output of the above code would be:
Hello John!
Hello Jane!
You've learned about Components and Props, two fundamental building blocks of any React Native application. You've also learned how to create and use Components, and how to pass and use Props to transfer data between Components.
For further learning, you could explore using Props with Class Components, Typechecking with PropTypes, and Default Prop Values.
UserProfile
component that accepts username
and age
as props and displays them.UserProfile
component to accept an address
prop and display it only if it's passed.Solutions:
1.
function UserProfile(props) {
return (
<View>
<Text>Username: {props.username}</Text>
<Text>Age: {props.age}</Text>
</View>
);
}
function UserProfile(props) {
return (
<View>
<Text>Username: {props.username}</Text>
<Text>Age: {props.age}</Text>
{props.address && <Text>Address: {props.address}</Text>}
</View>
);
}
In the second solution, {props.address && <Text>Address: {props.address}</Text>}
ensures that the address
is only displayed if it's passed as a prop.