Example of Realm Database in React Native

Example of Realm Database in React Native

Here is an Example of Realm Database in React Native. We are hoping you have read our previous post on Local Database in React Native App. A realm is an open-source object database management system, initially for mobile, also available for platforms such as Xamarin or React Native, and others, including desktop applications, and is licensed under the Apache License. We can perform all the CRUD transactions in this database and much faster than the SQLite database. Let’s get started with Realm.

In this Example, we will make a HomeScreen with the option to go to other screens like

  • RegisterUser: To Register the User. (Create/Insert)
  • ViewAllUser: To View All Users. (Read)
  • ViewUser: To View Singel Users By Id. (Read)
  • UpdateUser: To Update the User.(Update)
  • DeleteUser: To Delete the User.

We will be having some custom components like Mybutton, Mytext, Mytextinput which will be used in place of react-native Button, Text, and TextInput.

How to Use

You just have to import the library like this:

and open the database using

Now, whenever you need to make some database call you can use realm to execute the database query.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use Realm we need to install realm package.

To install this

Open the terminal and jump into your project using.

run the following command.

This command will copy all the dependencies into your node_module directory, You can find the directory in node_modulethe directory named realm.

Linking of Library

To use Realm library we have to make some changes in the following files:

Go to the RealmExample> android > app > build.gradle.
Scroll down and add the following dependency.

Open RealmExample> android > settings.gradle and add the following lines.

 

Open RealmExample> android > app > src > main > java > com > realmexample> MainApplication.java  and add the following lines in imports.

Scroll down and add the following line in getPackages after new MainReactPackage(). Please add a comma(,) and then add

 

Here we have done with our installation of the library. If you are still facing the trouble please comment below so that we can help you out from the problem.

In this Example, we are going to use createStackNavigator component to navigate so we need to install the react-navigation library. If you are new to the React Native Navigation then this post will help you.

To install react-navigation run the following command on terminal

Just to confirm the dependencies here is the screenshot of the dependencies that we are using ;

If you are on the same boat then let’s go further or please update your package.json and run npm install to install new dependencies.

For this Example, we are using the following structure. You have to make page directory in RealmExample and components directory under that. Make the marked js files too.

You can download the code from Github and can run directly

OR

Open the project directory and replace the following code

App.js

Mybutton.js

Mytext.js

Mytextinput.js

HomeScreen.js

RegisterUser.js

UpdateUser.js

ViewAllUser.js

ViewUser.js

DeleteUser.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

After running the Application successfully if you want to see the stored data in the device just to check whether database is storing the data proper you can visit How to See Realm Database Data Saved in Device using Android Studio.

             

This is how you can use Realm Database in React Native Application. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

3 thoughts on “Example of Realm Database in React Native”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.