In this tutorial, we aim to provide a step-by-step guide to getting started with React Native, a popular mobile app development framework. By the end of this tutorial, you should have a solid foundation in React Native and be able to create a simple app.
You will learn how to:
This tutorial assumes you have a basic understanding of JavaScript. Familiarity with React would be beneficial but is not required.
First, you need to install Node.js and npm (Node Package Manager). You can download them from the official Node.js website.
Once installed, you can install the React Native command line interface (CLI) globally on your machine using npm:
npm install -g react-native-cli
To create a new project, use the react-native init
command followed by your project name:
react-native init YourProjectName
Once the project is created, you will see several directories and files. The most important ones are:
index.js
: This is the entry point into your app.App.js
: This is where your main App component lives.android
and ios
directories: These contain files for your Android and iOS apps respectively.In App.js
, replace the existing code with the following:
import React from 'react';
import { Text, View } from 'react-native';
const App = () => {
return (
<View>
<Text>Hello, React Native!</Text>
</View>
);
};
export default App;
This code creates a simple component that displays "Hello, React Native!".
Let's add some interactivity to our app. We'll create a button that, when pressed, changes the text displayed.
Replace the code in App.js
with the following:
import React, { useState } from 'react';
import { Text, View, Button } from 'react-native';
const App = () => {
const [text, setText] = useState('Hello, React Native!');
return (
<View>
<Text>{text}</Text>
<Button title="Change Text" onPress={() => setText('Text Changed!')} />
</View>
);
};
export default App;
In this code:
useState
from React to create a state variable text
and a function setText
to update it.text
inside a Text
component.Button
component with the title "Change Text". When the button is pressed, we call setText
to change the value of text
to "Text Changed!".After running the app, you should see "Hello, React Native!" and a button. Pressing the button changes the text to "Text Changed!".
In this tutorial, we have covered:
Next, you may want to learn more about React Native components, styling, and state management. You can find more resources on the official React Native documentation.
Create a React Native app that displays a list of items using the FlatList
component. (Hint: You'll need to use the FlatList
component and pass an array of data to its data
prop.)
Modify the app from exercise 1 to display a button that, when pressed, adds a new item to the list. (Hint: You'll need to use the useState
hook to manage the list's state.)
Modify the app from exercise 2 to remove an item from the list when it is pressed. (Hint: You'll need to pass a function to the onPress
prop of each item that removes the item from the list's state.)
For each exercise, the solution should: