/
FirestoreProvider.js
39 lines (32 loc) · 1023 Bytes
/
FirestoreProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import FirestoreCache from './FirestoreCache';
export const FirestoreContext = React.createContext(null);
export default class FirestoreProvider extends Component {
static propTypes = {
firebase: PropTypes.object.isRequired,
children: PropTypes.node.isRequired,
useTimestampsInSnapshots: PropTypes.bool,
};
static defaultProps = {};
constructor(props) {
super(props);
const { firebase, useTimestampsInSnapshots } = props;
const firestore = firebase.firestore();
if (typeof useTimestampsInSnapshots !== 'undefined') {
const settings = { timestampsInSnapshots: useTimestampsInSnapshots };
firestore.settings(settings);
}
this.state = {
firestoreDatabase: firestore,
firestoreCache: new FirestoreCache(),
};
}
render() {
return (
<FirestoreContext.Provider value={this.state}>
{this.props.children}
</FirestoreContext.Provider>
);
}
}