Skip to content
the poor coder

Working Realm + React Navigator Example

Poor Man
Published
Reading time
3 min read
On this page

Working Realm + React Navigator Example#

When it comes to building mobile apps that require local storage, Realm is a great database option for React Native developers. In addition, React Navigation is an excellent library to use for managing navigation in your React Native applications. In this post, I will provide a working example of how to integrate Realm with React Navigation in a React Native app.

Step 1: Install Dependencies#

First, we need to install Realm and React Navigation packages in our project. We can do this by running the following commands in our terminal:

Bash

npm install --save realm react-navigation

Step 2: Create Realm Schema#

Next, we need to create a schema for our Realm database. In this example, we will create a simple schema with a single object and two properties: "id" and "name". We can create the schema in a separate file called "schema.js".

Here is an example of how the schema.js file would look like:

JavaScript

import Realm from 'realm';

class Person extends Realm.Object {}
Person.schema = {
    name: 'Person',
    primaryKey: 'id',
    properties: {
        id: 'int',
        name: 'string',
    },
};

export default new Realm({ schema: [Person] });

Step 3: Create Screens#

Now, we need to create screens for our app using React Navigation. In this example, we will create two screens: "HomeScreen" and "DetailsScreen". We can create these screens in separate files called "HomeScreen.js" and "DetailsScreen.js".

Here is an example of how the HomeScreen.js file would look like:

JavaScript

import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
import Realm from '../schema';

const HomeScreen = ({ navigation }) => {
    const [persons, setPersons] = useState([]);

    const fetchPersons = () => {
        const allPersons = Realm.objects('Person');
        setPersons(allPersons);
    };

    return (
        <View>
            <Text>Home Screen</Text>
            <Button title="Fetch Persons" onPress={fetchPersons} />
            {persons.map((person) => (
                <Text key={person.id}>{person.name}</Text>
            ))}
            <Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
        </View>
    );
};

export default HomeScreen;

Here is an example of how the DetailsScreen.js file would look like:

JavaScript

import React from 'react';
import { View, Text } from 'react-native';

const DetailsScreen = () => {
    return (
        <View>
            <Text>Details Screen</Text>
        </View>
    );
};

export default DetailsScreen;

Step 4: Add Routes to Navigation Stack#

Finally, we need to add our screens to the navigation stack using React Navigation. We can do this in a separate file called "App.js".

Here is an example of how the App.js file would look like:

JavaScript

import 'react-native-gesture-handler';
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';
import DetailsScreen from './screens/DetailsScreen';

const Stack = createStackNavigator();

const App = () => {
    return (
        <NavigationContainer>
            <Stack.Navigator>
                <Stack.Screen name="Home" component={HomeScreen} />
                <Stack.Screen name="Details" component={DetailsScreen} />
            </Stack.Navigator>
        </NavigationContainer>
    );
};

export default App;

And that's it! Now, we have a working example of how to integrate Realm with React Navigation in a React Native app. Of course, this is just a simple example, and you can customize it to fit your specific needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC