Chapter-5 React Native API, HTTP, Navigation, and Storage
Chapter-5 React Native API, HTTP, Navigation, and Storage
CHAPTER 5
REACT NATIVE API,
NAVIGATION, HTTP AND
STORAGE
10/10/2023
React Native HTTP
Many mobile apps need to load resources from a remote URL.
You may want to make a POST request to a REST API, or you may need to
fetch a chunk of static content from another server.
Using Fetch
React Native provides the Fetch API for your networking needs. Fetch will
seem familiar if you have used XMLHttpRequest or other networking APIs
before.
Making requests
In order to fetch content from an arbitrary URL, you can pass the URL to fetch:
fetch('https://mywebsite.com/mydata.json');
Fetch also takes an optional second argument that allows you to
customize the HTTP request. You may want to specify additional
headers, or make a POST request:
Handling the response
The above examples show how you can make a request. In many cases, you
will want to do something with the response.
Networking is an inherently asynchronous operation. Fetch method will
return a Promise that makes it straightforward to write code that works in
an asynchronous manner:
You can also use the async / await syntax in a React Native app:
Don't forget to catch any errors that may be thrown by fetch, otherwise they
will be dropped silently.
How to use async / await?
We use async-await to manage time consuming tasks using async
await we have the option to wait for the first task before executing the
second task.
Async keyword use for define function for use await task, when you want
to use to await for wait task then first you have to define async function.
Await keyword use for stop execution till task response, we will use
multiple await calls in one async function.
Syntax
Async syntax for normal function example
10/10/2023
Navigation types in React native
Drawer navigation
This consists of patterns in navigation that
require you to use a drawer from the left
(sometimes right) side for navigating
between and through screens.
Typically, it consists of links that provide
a gateway to moving between screens.
It’s similar to sidebars in web-based
applications.
Tab navigation
This is the most common form of
navigation in most mobile applications.
Navigation is possible through tab-based
components.
It consists of bottom and top tabs.
Stack navigation
Transition between screens is made
possible in the form of stacks.
Various screens are stacked on top of
each other, and movement between
screens involves replacing one screen
with another.
Most times, mobile applications make use of all types of navigators to
build the various screens and functionalities of the application. This is
called nesting navigators.
Installation and setup
First, you need to install them in your project:
npm install @react-navigation/native @react-navigation/native-stack
Next, install the required peer dependencies. You need to run different commands
depending on whether your project is an Expo managed project or a bare React
Native project.
If you have an Expo managed project, install the dependencies with expo:
expo install react-native-screens react-native-safe-area-context
If you have a bare React Native project, install the dependencies with npm:
npm install react-native-screens react-native-safe-area-context
If you're on a Mac and developing for iOS, you need to install the pods (via Cocoapods) to
complete the linking.
npx pod-install ios
Creating a simple stack navigation
Step:1- Import the following components
Persisting data in a mobile app has benefits such as when the user restarts
the app, the data or the setting variables being available to the user in the
state they left before closing the app.
Security
AsyncStorage encrypts none of the data saved. All objects are stored as
strings and must be serialized before they can be saved and likewise be de-
serialized before retrieval.
We can perform three main actions with AsyncStorage: Set, Get,
and Delete:
The setItem() method saves data to the AsyncStorage and allows a key and
a value. Here, the key is a string that the data or value is assigned to:
The getItem() method allows us to get data back from AsyncStorage by
using the key the data was saved as.
The removeItem() method removes data from AsyncStorage by using the
key to delete the stored data:
The clear() method deletes or removes all the data from AsyncStorage. It
does not need a key because it deletes all the stored data:
Installing the community module
npm install @react-native-async-storage/async-storage
Usage
Async Storage can only store string data, so in order to store object
data you need to serialize it first.
Fordata that can be serialized to JSON you can use
JSON.stringify() when saving the data and
JSON.parse() when loading the data.
Importing
import AsyncStorage from '@react-native-async-storage/async-storage';
Storing Data
setItem() is used both to add new data item (when no data for given key
exists), and to modify existing item (when previous data for given key exists).
Storing string value
Storing object value
Reading data
getItem returns a promise that either resolves to stored value when
data is found for given key, or returns null otherwise.
Reading string value
Reading object value
Persist data using SQlite
It’s very common for developers to use SQLite, a C-language
library, as the data-store in mobile applications.
SQLite is an open-source SQL database that stores data to a text file
on a device. It supports all the relational database features.
In order to access this database, we don’t need to establish any kind of
connections for it like JDBC, ODBC.
We only have to define the SQL statements for creating and updating the
database. Access to it involves accessing the file system. This can be slow.
Therefore, it is recommended to perform database operations asynchronously.
Installing the community module
npm install --save react-native-sqlite-storage
Database Setup
While working with React Native it provides two option:
You can make the database in runtime.
You can use the pre-populated database so that you can start working directly.
How to Use react-native-sqlite-storage?
Importing
import SQLite from ‘react-native-sqlite-storage’
Open the database
let db = SQLite.openDatabase({ name: 'UserDatabase.db' });
Basic operations
Creating table
Insert record
Update record
Delete record
Select record
Now, whenever you need to make some database call you can use db
variable to execute the database query.
56
Thank You
10/10/2023
57
Questions?
10/10/2023