react firebase database code example
Example 1: add firebase in react
import * as firebase from 'firebase'
const firebaseConfig = {
apiKey: "AIzaSyC6EDL8gMkZc3GGzGveMqWe5zvAr5DNiL4",
authDomain: "blood-components.firebaseapp.com",
databaseURL: "https://blood-components.firebaseio.com",
projectId: "blood-components",
storageBucket: "blood-components.appspot.com",
messagingSenderId: "388223113819",
appId: "1:388223113819:web:1273570a12add0fedafd7e",
measurementId: "G-K6NM078FWD"
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
const usersRef = db.collection('users')
const functions = firebase.functions();
const googleProvider = new firebase.auth.GoogleAuthProvider();
export { firebase, db, auth, functions, googleProvider }
Example 2: firebase react js
npm install firebase
Example 3: using firebase with react key value
componentDidMount() {
const itemsRef = firebase.database().ref('items');
itemsRef.on('value', (snapshot) => {
let items = snapshot.val();
let newState = [];
for (let item in items) {
newState.push({
id: item,
title: items[item].title,
user: items[item].user
});
}
this.setState({
items: newState
});
});
}