React.js / React Native Basics
Using Components and Props in React Native
This tutorial will delve into the building blocks of React Native applications - Components and Props. You will learn how to create components and pass data using props.
Section overview
5 resourcesIntroduces React Native for building cross-platform mobile applications.
Using Components and Props in React Native: A Comprehensive Guide
1. Introduction
Goal of the Tutorial
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.
Learning Outcomes
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
Prerequisites
A basic understanding of JavaScript and React Native is required. Familiarity with ES6 syntax (especially arrow functions) will be helpful.
2. Step-by-Step Guide
What are Components?
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.
What are Props?
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.
How to create a Component
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.
How to use Props
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" />
3. Code Examples
Let's see a practical example of using components and props in a React Native application.
Example 1: Creating and using a simple Component with Props
// 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!
4. Summary
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.
5. Practice Exercises
- Create a
UserProfilecomponent that acceptsusernameandageas props and displays them. - Modify the
UserProfilecomponent to accept anaddressprop 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.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article