1) Open account on Firebase
2) install firebase component to react native project using
npm install firebase --save
3) Create new account on Firebase URL https://console.firebase.google.com
then create new project
we will need the code in red area in the next file
React Native Code
4) Open Firebase DB connection on new page with name 'firebase.js'
5) Create code to use Firebase connection
Code does the next jobs: Create Account/Login/LogOut and also for Insert/Update/Select/Delete
2) install firebase component to react native project using
npm install firebase --save
3) Create new account on Firebase URL https://console.firebase.google.com
then create new project
we will need the code in red area in the next file
React Native Code
4) Open Firebase DB connection on new page with name 'firebase.js'
import * as firebase from 'firebase';
// Initialize Firebase
const firebaseConfig = {
apiKey: "xxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxx.firebaseapp.com",
databaseURL: "https://xxxxxxxxxx.firebaseio.com",
projectId: "xxxxxxxxx",
storageBucket: "xxxxxxxxxx.appspot.com",
messagingSenderId: "xxxxxxxxxxxx"
};
let instance = null
class FirebaseService {
constructor() {
try {
if (!instance) {
this.app = firebase.initializeApp(firebaseConfig);
instance = this;
}
return instance;
}
catch (e) {
console.log('Firebase error');
console.log(e);
return null;
}
}
}
const firebaseService = new FirebaseService().app
export default firebaseService;
5) Create code to use Firebase connection
Code does the next jobs: Create Account/Login/LogOut and also for Insert/Update/Select/Delete
import React, { Component } from 'react';
import { Text, View, StyleSheet, LayoutAnimation, Platform, UIManager, TouchableOpacity } from 'react-native';
import firebaseService from './firebase'
export default class FireBaseTest extends React.Component {
componentWillMount() {
//Create account
// firebaseService.auth().createUserWithEmailAndPassword('test@test.com', '123456').catch(function(error) {
// console.log(error);
// });
//Login
// firebaseService.auth().signInWithEmailAndPassword('test@test.com', '123456').catch(function(error) {
// console.log(error);
// });
console.log('Code version 2');
//Insert
firebaseService.database().ref('News/001').set({
Title: 'Test',
Contents: 'Test'
}).then(() => {
//console.log('Insert Done');
}).catch((error) => {
// console.log(error);
});
//Update
firebaseService.database().ref('News/001').update({
Title: 'Update Title Only!'
}).then(() => {
//console.log('Update Done');
}).catch((error) => {
//console.log(error);
});
//=========Select
//Method 1
//Run only once during loading
firebaseService.database().ref('News').once('value', (data) => {
var returnResults = data.toJSON();
console.log(returnResults);
}).then(() => {
//console.log('Select Done');
}).catch((error) => {
//console.log(error);
});
//Method 2
//Run with everytime DB data change
// firebaseService.database().ref('News').on('value', (data) => {
// var returnResults = data.toJSON();
// console.log(returnResults);
// });
//Delete
// firebaseService.database().ref('News/001').remove().then(() => {
// console.log('Done');
// }).catch((error) => {
// console.log(error);
// });
//SignOut
// firebase.auth().signOut().then(function() {
// console.log('Sign-out successful');
// }).catch(function(error) {
// console.log(error);
// });
}
render() {
return (
<View>
<View>
<TouchableOpacity>
<Text>Title</Text>
</TouchableOpacity>
<View>
<Text >
Contents
</Text>
</View>
</View>
</View>
);
}
}
No comments:
Post a Comment