react-firestore-base
Why use this? I like to keep things simple and I think this is the simplest react implementation. Firestore stores data in either a collection or a document, so the base classes reflect that.
Document
For a document, you want the data. the base class will add the data from the cloud into the components state.
Collections
for a collection, you may want 1 of 2 things:
- a reference to the documents, to use in the above
Document
- a collection of the document IDs, this is useful if your document names aren't arbitrary.
Usage
Setting up Firestore
this library does not setup firebase for you, you need to define it yourself, inline with the google docs. I would recommend setting it up in your root react file and add it to our apps context. that way child elements will have access to it.
;firebase const db = firebase; { return db } { return <div>your app</div> } AppchildContextTypes = db: PropTypesobject
Document
Defining the component
Here we are defining that our component will extend the Document class. This keeps our class very clean. The only thing to note is that you have to define a default state but this is standard React.
{ super thisstate = data: '' } { return <p>thisstatedata</p> }
Using the component
; { const documentRef = thiscontextdb return <MyDocument document=documentRef /> } WrappercontextTypes = db: PropTypesobject ;
CollectionRef
Defining the component
{ return thisstatedocuments }
Using the component
; { const collectionRef = thiscontextdb return <MyCollectionRef collection=collectionRef /> } WrappercontextTypes = db: PropTypesobject ;
CollectionId
Defining the component
{ return thisstatedocuments }
Using the component
; { const documentRef = thiscontextdb return <MyCollectionId document=documentRef /> } WrappercontextTypes = db: PropTypesobject ;
collections
WHERE
; { const collectionRef = thiscontextdb return <MyCollectionRef where='public' '==' true collection=collectionRef /> } WrappercontextTypes = db: PropTypesobject ;
ORDERBY
; { const collectionRef = thiscontextdb return <MyCollectionRef orderBy="createdAt" "desc" collection=collectionRef /> } WrappercontextTypes = db: PropTypesobject ;
LIMIT
; { const collectionRef = thiscontextdb return <MyCollectionRef limit=10 collection=collectionRef /> } WrappercontextTypes = db: PropTypesobject ;
documents
updating a document
{ super thisstate = data: '' } { thispropsdocument } { return <div> thisstatedata <button onClick=thishandleUpdate>update</button> </div> }
deleting a document
{ super thisstate = data: '' } { thispropsdocument } { return <div> thisstatedata <button onClick=thishandleDelete>delete</button> </div> }