Reputation: 2046
Trying to user firebase.firestore
with react. Here is how I initialize firebase.
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import store from './redux'
import Routes from './Routes'
import initFirebase from './initFirebase'
class App extends Component {
componentWillMount () {
initFirebase()
}
render () {
return <Provider store={store}>
{Routes}
</Provider>
}
}
export default App
And here is initFirebase.js
import firebase from 'firebase'
import store from './redux'
import { setAuth } from './redux/actions/auth'
export default () => {
// Initialize Firebase
var config = {
apiKey: 'AIzaSyC1H97dDoIVurLdgHOGgfRRubrTmb3YkTo',
authDomain: 'tree-of-life-ee870.firebaseapp.com',
databaseURL: 'https://tree-of-life-ee870.firebaseio.com',
projectId: 'tree-of-life-ee870',
storageBucket: 'tree-of-life-ee870.appspot.com',
messagingSenderId: '861153906067'
}
firebase.initializeApp(config)
firebase.auth().onAuthStateChanged(user => {
store.dispatch(setAuth(user))
})
}
And here I try to use firestore
like this
import firebase from 'firebase'
import { SubmissionError } from 'redux-form'
import { browserHistory } from 'react-router'
const db = firebase.firestore()
And get the error like this
Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
So what's the right way to initialize firebase in a react enviorment?
Upvotes: 0
Views: 1304
Reputation: 23
You need initialize firebase before call firebase.firestore()
You can move
import firebase from 'firebase';
firebase.initializeApp(config);
to index.js
file.
Upvotes: 1