Created
January 17, 2020 04:41
-
-
Save dmurawsky/ddb09c720dbfd6cbaa2bee43151db3e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import firebase from "firebase/app"; | |
import "firebase/database"; | |
export const withFirebase = comp => { | |
comp.componentDidMount = () => { | |
if (!firebase.apps.length) { | |
firebase.initializeApp({ | |
apiKey: "", | |
authDomain: "", | |
databaseURL: "", | |
projectId: "", | |
appId: "" | |
}); | |
} | |
}; | |
return comp; | |
}; | |
export const listenPath = (path, cb) => { | |
const snapCb = snap => cb(snap.val()); | |
const ref = firebase.database().ref(path); | |
ref.on("value", snapCb); | |
return () => ref.off("value", snapCb); | |
}; | |
export const setPath = (path, value) => | |
firebase | |
.database() | |
.ref(path) | |
.set(value); | |
export const updatePath = (path, update) => | |
firebase | |
.database() | |
.ref(path) | |
.update(update); | |
export const pushPath = (path, value) => | |
firebase | |
.database() | |
.ref(path) | |
.push(value); | |
export const removePath = path => | |
firebase | |
.database() | |
.ref(path) | |
.remove(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment