Created
October 30, 2020 01:19
-
-
Save ryanflorence/26f78036ad6275c20453b2aefb70ac57 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
/////////////////////////////////////////////////////////// | |
// loader | |
const { db } = require("../server/firebase"); | |
module.exports = () => { | |
return db.doc("some/path").get().data(); | |
}; | |
/////////////////////////////////////////////////////////// | |
// Non-remix hook for docs, takes initial data for immediate | |
// rendering | |
import React from "react"; | |
import { db } from "./client/firebase"; | |
export function useDoc(path, initialData = null) { | |
let [state, setState] = React.useState({ | |
status: "loading", | |
doc: initialData, | |
}); | |
React.useEffect(() => { | |
return db.doc(path).onSnapshot((doc) => { | |
setState({ | |
status: "subscribed", | |
data: doc.data(), | |
}); | |
}); | |
}, [path]); | |
return state; | |
} | |
/////////////////////////////////////////////////////////// | |
// Route | |
import { useRouteData } from "@remix-run/react"; | |
export default function SomeRoute() { | |
let initialData = useRouteData(); | |
let data = useDoc("some/path", initialData); | |
return ( | |
<div> | |
<h1>I'm live!</h1> | |
<div>{data.whatever}</div> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment