Skip to content

Commit a9a9892

Browse files
authored
fix(examples): add firebase as a dependency to complete/simple example (#866) - @dzoba
chore(examples): add emulator support to simple example
2 parents c541b4e + 26df4d5 commit a9a9892

File tree

8 files changed

+3525
-30053
lines changed

8 files changed

+3525
-30053
lines changed

examples/complete/simple/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ It correctly bundles React in production mode and optimizes the build for the be
6262
The build is minified and the filenames include the hashes.<br>
6363
Your app is ready to be deployed!
6464

65+
## Emulator
6566

66-
### Integrating Auth
67+
The database emulators provided by Firebase allow you to run the full application on a database the is emulated locally. This means that the data will be cleared each time you start the emulator. To use the emulator run the following:
68+
69+
1. Start the emulators: `yarn emulate`
70+
1. Start the app: `yarn dev`
71+
72+
## Integrating Auth
6773

6874
Checkout [the auth recipes](../../../docs/recipes/auth.md) for some simple examples of how to integrate auth.
6975

examples/complete/simple/firebase.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@
1515
"destination": "/index.html"
1616
}
1717
]
18+
},
19+
"emulators": {
20+
"database": {
21+
"port": 9000
22+
},
23+
"firestore": {
24+
"port": 8080
25+
}
1826
}
1927
}

examples/complete/simple/package-lock.json

Lines changed: 0 additions & 28839 deletions
This file was deleted.

examples/complete/simple/package.json

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,38 @@
66
"start": "react-scripts start",
77
"build": "react-scripts build",
88
"test": "react-scripts test",
9-
"eject": "react-scripts eject"
9+
"eject": "react-scripts eject",
10+
"emulate": "firebase emulators:start --only firestore,database",
11+
"dev": "cross-env REACT_APP_USE_DB_EMULATORS=true yarn start"
1012
},
1113
"dependencies": {
14+
"firebase": "^7.8.2",
1215
"prop-types": "^15.7.2",
13-
"react": "16.10.0",
14-
"react-dom": "16.10.0",
16+
"react": "16.12.0",
17+
"react-dom": "16.12.0",
1518
"react-redux": "^7.1.0",
16-
"react-redux-firebase": "next",
19+
"react-redux-firebase": "*",
1720
"redux": "^4.0.4",
18-
"redux-firestore": "^0.9.0",
21+
"redux-firestore": "^0.12.0",
1922
"redux-thunk": "^2.3.0"
2023
},
2124
"devDependencies": {
22-
"react-scripts": "3.1.0"
25+
"cross-env": "^7.0.0",
26+
"react-scripts": "3.4.0"
2327
},
2428
"eslintConfig": {
2529
"extends": "react-app"
2630
},
27-
"browserslist": [
28-
">0.2%",
29-
"not dead",
30-
"not ie <= 11",
31-
"not op_mini all"
32-
]
31+
"browserslist": {
32+
"production": [
33+
">0.2%",
34+
"not dead",
35+
"not op_mini all"
36+
],
37+
"development": [
38+
"last 1 chrome version",
39+
"last 1 firefox version",
40+
"last 1 safari version"
41+
]
42+
}
3343
}

examples/complete/simple/src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import { ReactReduxFirebaseProvider } from 'react-redux-firebase';
88
import { createFirestoreInstance } from 'redux-firestore';
99
import Home from './Home'
1010
import configureStore from './store'
11-
import { firebase as fbConfig, reduxFirebase as rfConfig } from './config'
11+
import initFirebase from './initFirebase'
12+
import { reduxFirebase as rfConfig } from './config'
1213
import './App.css'
1314

1415
const initialState = window && window.__INITIAL_STATE__ // set initial state here
1516
const store = configureStore(initialState)
1617
// Initialize Firebase instance
17-
firebase.initializeApp(fbConfig)
18+
initFirebase()
1819

1920
export default function App () {
2021
return (

examples/complete/simple/src/NewTodo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function NewTodo() {
1616
}
1717

1818
return (
19-
<div>
19+
<div style={{ marginBottom: '4rem' }}>
2020
<h4>New Todo</h4>
2121
<input value={inputVal} onChange={onInputChange} />
2222
<button onClick={addTodo}>Add</button>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import firebase from 'firebase/app'
2+
import 'firebase/auth'
3+
import 'firebase/database'
4+
import 'firebase/firestore' // make sure you add this for firestore
5+
import { firebase as fbConfig } from './config'
6+
7+
let firebaseInstance
8+
9+
export default function initFirebase(initialState, history) {
10+
if (firebaseInstance) {
11+
return firebaseInstance
12+
}
13+
14+
// Initialize firebase instance if it doesn't already exist
15+
if (!firebaseInstance) {
16+
const shouldUseEmulator = process.env.REACT_APP_USE_DB_EMULATORS
17+
18+
if (shouldUseEmulator) { // or window.location.hostname === 'localhost' if you want
19+
console.log('Using RTDB emulator')
20+
fbConfig.databaseURL = `http://localhost:9000?ns=${fbConfig.projectId}`
21+
}
22+
23+
// Initialize Firebase instance
24+
firebase.initializeApp(fbConfig)
25+
26+
if (shouldUseEmulator) { // or window.location.hostname === 'localhost' if you want
27+
console.log('Using Firestore emulator')
28+
firebase.firestore().settings({
29+
host: 'localhost:8080',
30+
ssl: false
31+
})
32+
}
33+
firebaseInstance = firebase
34+
}
35+
36+
37+
return firebaseInstance
38+
}

0 commit comments

Comments
 (0)