From c5dbbbca5a2ed5a0070c3e9f53bbbf1a40abdd75 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 12 Nov 2018 16:00:22 +0100 Subject: [PATCH] fix(FirestoreProvider): Added prop to use timestamps in snapshots for Firestore > v5.5.0 (#32) --- README.md | 6 ++++++ src/FirestoreProvider.js | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 774a448..7b22872 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ At the top level of your app, configure `firebase` and render the If you're using [create-react-app][create-react-app], your `index.js` file would look something like this: + ```jsx import React from 'react'; import ReactDOM from 'react-dom'; @@ -108,6 +109,11 @@ ReactDOM.render( ); ``` +_Important: Starting with Firebase v5.5.0 timestamp objects stored in Firestore get returned as Firebase +Timestamp objects instead of regular Date() objects. To make your app compatible with this +change, add the `useTimestampsInSnapshots` to the FirestoreProvider element. If you dont do this +your app might break. For more information visit [the Firebase refrence documentation][https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp]._ + _Note: The reason for the separate imports for `@firebase/app` and `@firebase/firestore` is because `firestore` is not included in the default `firebase` wrapper package. See the [firestore package](https://www.npmjs.com/package/@firebase/firestore) for more details._ diff --git a/src/FirestoreProvider.js b/src/FirestoreProvider.js index 9ac001d..491c403 100644 --- a/src/FirestoreProvider.js +++ b/src/FirestoreProvider.js @@ -6,8 +6,13 @@ export default class FirestoreProvider extends Component { static propTypes = { firebase: PropTypes.object.isRequired, children: PropTypes.node.isRequired, + useTimestampsInSnapshots: PropTypes.bool.isRequired }; + static defaultProps = { + useTimestampsInSnapshots: false + } + static childContextTypes = { firestoreDatabase: PropTypes.object.isRequired, firestoreCache: PropTypes.object.isRequired, @@ -19,7 +24,7 @@ export default class FirestoreProvider extends Component { const { firebase } = props; this.state = { - firestoreDatabase: firebase.firestore(), + firestoreDatabase: firebase.firestore({timestampsInSnapshots: this.props.useTimestampsInSnapshots}), firestoreCache: new FirestoreCache(), }; }