green-arrow / react-firestore Goto Github PK
View Code? Open in Web Editor NEWReact components to fetch data from firestore using render props
License: MIT License
React components to fetch data from firestore using render props
License: MIT License
Is anyone working on adding support for the local firebase emulator? I would like to be able to pass prop to indicate I want tp connect to the local firestore emulator for local testing and CI/CD scripts.
react-firestore
version: 0.10.3FirestoreCollection
This component allows you to interact with a Firestore collection. Using this component, you can access the collection at a given path and provide sort options, perform queries, and paginate data.
What would be the idiomatic way of paginating? limit
is exposed, but I don't see startAt
or startAfter
in the interface of <FirestoreCollection>
https://firebase.google.com/docs/firestore/query-data/query-cursors
Thanks for providing a nice library!
react-firestore
version: 0.8.1 (from getfirefly.org)node
version: 12.13.1npm
(or yarn
) version: 6.13.6Relevant code or config
<FirestoreDocument path={`posts/${match.params.id}`}>
What you did: Query a document with an id that doesn't exist.
What happened:
Instead of getting an error (necessary for displaying a 404), it just returns the data with the id I provided it.
Suggested solution: Provide an error when the document isn't found.
react-firestore
version: @latestnode
version: 8.11.1npm
(or yarn
) version: 6.0.0Relevant code or config:
n/a
I was just wondering if there was a way to query a random document?
E.g. when doing filter={["thing", "==", db.doc("things/" + thing)]}
. I think this is because deepEqual
thinks they’re always different, but I haven’t investigated in detail.
See also #19
Sorry for not using the issue template... I just have a really small question. I'm using the library for some queries and stuff and I wanted to show a message when an error popped up. I noticed this awesome commit of like 12 days ago which solves this issue and I wonder when this will be deployed to NPM and everything. Thanks!
Is anyone working on adding support for the local firebase emulator? I would like to be able to pass prop to indicate I want tp connect to the local firestore emulator for local testing and CI/CD scripts.
Great work with the library 👍 . Currently using with react-native-firebase
. Not sure if my issue is related only to mobile but, I am not able to perform a .where
in conjunction with a .order
and have the collections listen to real time updates.. No problem, I've decided to just do my sorting in my components..
It would be nice to add in a sort prop to clean up this logic that gets applied after receiving a collection so I don't have to do this as well.
The biggest issue with this is currently there is no way to stop showing the loading after some timeout. If there is not data in a collection this loads indefinitely.. which isn't the best UX. We should be able to provide a timeout to stop showing the loading after a certain amount of time (while still having it listen to updates come in). I have a solution that works just fine, but it would be nice to just have this here in this module.
I'd be willing to create a PR for this if you think both or one of these suggestions would be beneficial.
Let me know!
Hi @green-arrow, this is a truly awesome project!
I wanted to ask, is there any place in the docs which would guide me towards combining collections?
For instance, in the case where I have two collections. One has property 'collection1.userId' and collection 2 also contains this: 'collection2.userId'.
I would like to query all of collection1 and combine to each document, the matching document from collection2.
In super rough pseudo code, the idea is:
FirestoreCollection='users'
userId
firstName
lastName
FirestoreCollection='relational-collection'
userId=='userId'
get related document
Allow the user to specify the total number of documents that should be returned, as well as an offset, so that the user can support paging through a large number of documents.
Last one I promise :)
This one is really a matter of opinion, but I think rendering to this.props.children instead of a "render" prop makes things a little cleaner.
The React docs describe using this method: https://reactjs.org/docs/render-props.html#using-props-other-than-render
Just my 2¢
// CURRENT
<FirestoreCollection
path="stories"
sort="publishedDate:desc,authorName"
render={({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
);
}}
/>
// PROPOSED
<FirestoreCollection path="stories" sort="publishedDate:desc,authorName">
{({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
);
}}
</FirestoreCollection>
I know this isn't a Firestore request, but I'm guessing a lot of people who use this package would appreciate a component with a similar API to expose the state of Firebase Auth. (I know I would.)
Sample usage:
import { FirebaseAuth } from 'react-firestore'
<FirebaseAuth>
{ ({error, isLoading, auth}) => {
if (error) {
return 'error logging you in'
}
if (isLoading) {
return 'loading account...'
}
if (!auth) {
return <LoginButton />
} else {
return <UserSummary user={auth} />
}
}}
</FirebaseAuth>
Something like this could work: https://github.com/sampl/firefly/blob/master/src/data/AuthProvider.js
What do you think @green-arrow?
Make sure the preact build works properly
Currently using a number to filter a collection query causes a PropType error, when it is actually valid. In fact, changing the number to a string to silence the warning breaks the filter.
I'm happy to contribute the fix for this, if you can confirm that this project is still being maintained? Hope so!
react-firestore
version: 0.9.0node
version: v10.1.0npm
(or yarn
) version: 6.1.0I am getting a console error regarding timestampsInSnapshots and i it is a big bright red wall of text. Where should I set it?
Here is a basic project using react-firestore
Test that the build process works properly and the built files can be imported and used.
👋
Would love to use the new Firestore collection group feature in react-firestore
https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query
Add a withFirestore
component that will inject the firestore database (from firebase.firestore()
) into a component.
This provides an escape hatch that will allow the user to directly access firestore in cases where the other components don't completely fulfill the user's needs.
react-firestore
version: 1.0.1node
version: 10.16.0npm
(or yarn
) version: 6.8.0I use react together with typescript, for ease of typing and being native (I use VS Code) when comparing with Flow. It would be interesting to create a index.d.ts
file containing the declared types of the library, so that it would be possible to make the most of the resources being created ...
I searched the types defined for the library but did not find it. I also searched within the repository itself and did not find it. I believe that if they were created, it would be possible to work more easily and clearly ...
Allow the user to specify one or more filter criteria that will be sent to firestore using the where
method off of the Query object.
Another feature request--would be really nice to have the option to pass a loading component directly to the react-firestore components.
This could co-exist with the current method of passing isLoading in the render method, letting people choose.
// CURRENT
<FirestoreCollection
path="stories"
sort="publishedDate:desc,authorName"
render={({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
);
}}
/>
// PROPOSED
<FirestoreCollection
path="stories"
sort="publishedDate:desc,authorName"
loading={Loading}
render={({ data }) => (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
)}
/>
To make the API even cleaner, you could let users set a default loading component so it only has to be passed in special cases (similar solution).
// user component (no loading code at all)
<FirestoreCollection
path="stories"
sort="publishedDate:desc,authorName"
render={({ data }) => (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
)}
/>
// FirestoreCollection component
render() {
if (this.state.loading) {
return this.props.loading || 'loading...' // or replace 'loading...' with a user-set global default loading component
}
...
}
Hopefully these are useful suggestions! Let me know if I should format differently or hold off submitting until I have a PR.
If the document does not exist, you get this in data
: {id: "undefined"}
(yes, the string "undefined"
, not the identifier!). This makes it rather difficult to check.
I would expect data
just to be null
if the document does not exist.
Is there an option for react-firebase
to support delta change of QuerySnapshot
to cherry-pick and update only the affected documents (added, modified, deleted) in the local copy?
react-firestore
version: 1.0.1node
version: v8.16.0npm
(or yarn
) version: 1.19.1Relevant code or config
The current FirebaseCollection.handleOnSnapshotSuccess()
is directly updating the state by replacing the data
in case of even minor updates of the collection.
What you did:
MUI-Datatable wrapped in FirebaseCollection and data
is shared between the components.
What happened:
Upon internal (from within the app) or external change of the data (another app user) the Datatable widget will re-render fully.
Problem description:
In case consumer widget is relying on state of data
the component must re-render fully breaking the flow unexpectedly. In my app the FirebaseCollection feeds MUI-Datatable and change of underlying data re-renders the full table and loosing the state of the table.
Suggested solution:
docChange()
and a different state management approach,useReducer
to delegate state management to consumer,react-firestore
version: 0.10.1firebase
version: 5.5.8node
version: 8npm
(or yarn
) version: 1.7 yarnRelevant code or config
import firebase from '@firebase/app';
import '@firebase/firestore';
firebase.initializeApp(firebaseConfig);
<FirestoreProvider firebase={firebase} useTimestampsInSnapshots>
What you did:
It seems like the latest version of the package isn't initialising correctly due to how the timestamp settings are being set.
What happened:
Throwing the following error on load:
Firebase: firebase.firestore() takes either no argument or a Firebase App instance. (app/invalid-app-argument).
Suggested solution:
It seems to be caused by this line: https://github.com/green-arrow/react-firestore/blob/master/src/FirestoreProvider.js#L27
Untested, but I think it needs to be replaced with firebase.firestore().settings({ timestampsInSnapshots: this.props.useTimestampsInSnapshots})
before Line 26
Versions
Relevant code or config (meta code)
let firebase = ...;
export default () => (
<FirestoreProvider firebase={firebase}>
<FirestoreCollection path={'mycollection'} ... />
</FirestoreProvider>
)
What you did:
Following the readme instructions basically to simply render a FirestoreCollection somewhere under the FirestoreProvider.
What happened:
Blank screen, complaining for firestoreDatabase childProp being null although it's required.
Problem description:
Apparently, firebase's firestore api now returns null from the settings(...) call (which I guess was wrong before a certain version.
See https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore-types/index.d.ts#L96
Suggested solution:
Instead of returning from the .settings() call, just do it on its own line and return the firestore instance.
https://github.com/green-arrow/react-firestore/blob/master/src/FirestoreProvider.js#L28
(fix works locally when patching the provider code, will provide a simple pull request)
If someone (me or anyone else) would like to add Typescript support to react-firestore
, would you prefer:
From a TS user perspective the solution 1/ is preferable since it prevents type definition to drift appart from actual library but it requires that the library is maintained in TS (which has numerous benefits :))
👋
First of all, thanks for releasing react-firestore. It's the perfect way to bring my two favorite tools together, and the API you chose is spot on.
A feature request: it would be great if each component could pass Firebase errors when they occur. This can happen if you try to query content but don't have the right permissions, for example.
Something like:
<FirestoreCollection
path="stories"
sort="publishedDate:desc,authorName"
render={({ isLoading, data, error }) => {
if (error) return `Whoops! ${error.message}`
return isLoading ? (
<Loading />
) : (
<div>
<h1>Stories</h1>
<ul>
{data.map(story => (
<li key={story.id}>
{story.title} - {story.authorName}
</li>
))}
</ul>
</div>
);
}}
/>
I've built something similar here: https://github.com/sampl/firefly/blob/master/src/data/PostsProvider.js
react-firestore
version: 1.0.1node
version: v10.19.0npm
(or yarn
) version: yarn 1.21.1react
version: 16.12.0What happened:
From the browser console when using react-firestore with recent versions of React:
Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.
react-firestore
version: 0.6.0node
version: 8.9.0npm
(or yarn
) version: 5.5.1Relevant code or config
<FirestoreCollection
path="leagues"
render={({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<div className="form-group">
<label htmlFor="leagueId">League</label>
<select
className="form-control"
name="leagueId"
value={this.state.leagueId}
onChange={this.updateVal}
>
<option value="">Please select a league...</option>
{data &&
data.map(league => (
<option key={league.id} value={league.id}>
{league.name}
</option>
))}
</select>
</div>
);
}}
/>
<FirestoreCollection
path="teams"
filter={["leagueId", "==", this.state.leagueId]}
render={({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<div className="form-group">
<label htmlFor="teamId">Team</label>
<select
className="form-control"
name="teamId"
value={this.state.teamId}
onChange={this.updateVal}
>
<option value="">Please select a team...</option>
{data &&
data.map(team => (
<option key={team.id} value={team.id}>
{team.name}
</option>
))}
</select>
</div>
);
}}
/>
What you did:
I have 2 form elements. When the user selects a league, I want the team select to update with that particular leagues teams.
What happened:
The team dropdown never updates to reflect leagueId changes because the FirestoreCollection
listener is only set up on mount, instead of prop change.
Reproduction repository:
Problem description:
Suggested solution:
FirestoreCollection
and FirestoreDocument
should be refactored to update on prop change.
react-firestore
version: 0.10.3I want to show all elements in a list, which has not an empty string.
This works, it shows me only empty Strings
filter={['name', '==', '']}
But this doesn't I'm getting: INTERNAL ASSERTION FAILED: Unknown relation: !=
filter={['name', '!=', '']}
I'm observing the following console error which appears to be coming from react-firestore:
index.js:2178 [2018-05-09T08:43:10.595Z] @firebase/firestore: Firestore (4.13.0):
The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the
following code to your app before calling any other Cloud Firestore methods:
const firestore = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);
With this change, timestamps stored in Cloud Firestore will be read back as
Firebase Timestamp objects instead of as system Date objects. So you will also
need to update code expecting a Date to instead expect a Timestamp. For example:
// Old:
const date = snapshot.get('created_at');
// New:
const timestamp = snapshot.get('created_at');
const date = timestamp.toDate();
Please audit all existing usages of Date when you enable the new behavior. In a
future release, the behavior will change to the new behavior, so if you do not
follow these steps, YOUR APP MAY BREAK.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.