Package : react-native-firebase
Recommended Version : react-native-firebase@5.0.3
Firebase Database:
Firebase has all the functionality which is enough to develop simple and small application without having server.Using this package, datas can be stored in the Firebase database with ease.
Firebase database is a non structural database.
Create Project In Firebase:
Create a new project in Firebase Console.Enter Project Name and click create project.
New project will be created.
Configuring Firebase with Mobile App:
Click Project Overview.There you can see </> icon.
Click the icon and copy the config variable.
It will look like this.
<script src="https://www.gstatic.com/firebasejs/5.8.2/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyDV9mrqAAXyrKzq0BHxqlvG2Tedt-XqMRY",
authDomain: "clientapp-70f59.firebaseapp.com",
databaseURL: "https://clientapp-70f59.firebaseio.com",
projectId: "clientapp-70f59",
storageBucket: "clientapp-70f59.appspot.com",
messagingSenderId: "392055886880"
};
firebase.initializeApp(config);
</script>
Initializing database in mobile app:
Initialize firebase object by giving database and project reference.Put the following code inside your app.js or index.js or whichever is the starting point of your application.
var config = {
databaseURL: "",
projectId: "",
};
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
Best to place to initialize the app is on app loading.componentDidMount is suitable for that
componentDidMount( ) {
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
}
Now the project is initialized.Enable permission to read and write in database:
By default, permission to read and write the database is disabled.Enable the permission manually.
Go to Develop => Database.
Click Rules Tab.
You can see
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
Change the read and write to true to enable permission.
“rules” : {
“.read”:”true”,
“.write”:true
}
Writing to database:
firebase.database().ref('Users/').set({
“Email”: email,
“FName”: fname,
“LName”:lname
}).then((data)=>{
console.log('data ' , data)
}).catch((error)=>{
console.log('error ' , error)
})
Push data in database:
firebase.database().ref('UsersList/').push({
“Email”: email,
“FName”: fname,
“LName”:lname
}).then((data)=>{
//success callback
console.log('data ' , data)
}).catch((error)=>{
//error callback
console.log('error ' , error)
})
Read data from database:
firebase.database().ref('Users/').once('value', function (snapshot) {
console.log(snapshot.val())
});
}
It will read data once from the `Users` object and print it on console. If we want to get data whenever there is any change in it, we can use on instead of once
readUserData() {
firebase.database().ref('Users/').on('value', function (snapshot) {
console.log(snapshot.val())
});
}
Update database:
To update the data of any of the object, you need to create reference of it and use update() with the data that you want to change.
updateSingleData(email){
firebase.database().ref('Users/').update({
email,
});
}
Delete from database:
By just calling remove() from the reference of database will delete that record. You can even use null with set() or update() to delete that record from the database.
deleteData(){
firebase.database().ref('Users/').remove();
}