Skip to content

Instantly share code, notes, and snippets.

@dmurawsky
Created January 17, 2020 04:41
Show Gist options
  • Save dmurawsky/ddb09c720dbfd6cbaa2bee43151db3e9 to your computer and use it in GitHub Desktop.
Save dmurawsky/ddb09c720dbfd6cbaa2bee43151db3e9 to your computer and use it in GitHub Desktop.
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