Tianhao Zhou
Tianhao Zhou

Reputation: 822

How to use firebase features in react native

I want to write a mobile app using react native. And I want to use firebase because it saves me a lot of time. I used firebase before for my web app and it worked pretty well. Now that react native is actually a real native app, I am thinking if I can use firebase features for my app. For example, third-party authentication, deep link, etc. I know that even though all written in Javascript, react native is not web app at all. There are specifically several features I want to use. Google Auth, Cloud Messaging, and some Analytics features. Is there any way I can put them in my app as if I am writing my app in Swift or Java?

Upvotes: 0

Views: 303

Answers (1)

Ashiq Muhammed
Ashiq Muhammed

Reputation: 396

First of all you might need to add firebase under dependencies in package.json file

"dependencies": {
    "firebase": "^3.9.0",
    "react": "^16.0.0-alpha.6",
    "react-native": "0.44.0",
  },

After that Import firebase

import * as firebase from 'firebase';

Then do the usual firebase initialization

const config = {
        apiKey: "xxxxxxxxxx-xxxxxxxxxxxxx",
        authDomain: "xxxxxxxxxxx.firebaseapp.com",
        databaseURL: "https://xxxxxxxxxx.firebaseio.com",
        projectId: "xxxxxxxxxx",
        storageBucket: "xxxxxxxxxx.appspot.com",
        messagingSenderId: "xxxxxxxxxx"
      };
const firebaseApp = firebase.initializeApp(config);

Now that have connected firebase to your react-native app, you can retrieve the contents from the database like this

firebaseApp.database().ref("xxxxxx").on('value', (snap) => {
        console.log(snap.val());
});

The remaining can be easily figured out similarly

If you still need more help you can check out the following link : https://firebase.googleblog.com/2016/01/the-beginners-guide-to-react-native-and_84.html

Upvotes: 1

Related Questions