|
1 | | -# Thunks |
| 1 | +# redux-thunk Integration |
2 | 2 |
|
3 | | -### redux-thunk integration |
| 3 | +## getFirebase As Extra Argument |
4 | 4 |
|
5 | 5 | In order to get the most out of writing your thunks, make sure to set up your thunk middleware using its redux-thunk's `withExtraArgument` method like so: |
6 | 6 |
|
| 7 | +**createStore.js** |
| 8 | + |
7 | 9 | ```javascript |
8 | 10 | import { applyMiddleware, compose, createStore } from 'redux'; |
9 | 11 | import thunk from 'redux-thunk'; |
10 | | -import { reactReduxFirebase } from 'react-redux-firebase'; |
| 12 | +import { getFirebase } from 'react-redux-firebase' |
11 | 13 | import makeRootReducer from './reducers'; |
12 | 14 |
|
13 | 15 | const fbConfig = {} // your firebase config |
14 | | - |
| 16 | +const middlewares = [ |
| 17 | + thunk.withExtraArgument(getFirebase) |
| 18 | +] |
15 | 19 | const store = createStore( |
16 | 20 | makeRootReducer(), |
17 | 21 | initialState, |
18 | 22 | compose( |
19 | | - applyMiddleware(thunk), |
20 | | - reactReduxFirebase(fbConfig, { userProfile: 'users', enableLogging: false }) |
| 23 | + applyMiddleware(...middlewares), |
21 | 24 | ) |
22 | 25 | ); |
| 26 | +``` |
| 27 | + |
| 28 | +**App.js** |
| 29 | +```js |
| 30 | +import React from 'react' |
| 31 | +import { Provider } from 'react-redux' |
| 32 | +import firebase from 'firebase/app' |
| 33 | +import 'firebase/auth' |
| 34 | +import 'firebase/database' |
| 35 | +import 'firebase/firestore' // make sure you add this for firestore |
| 36 | +import { ReactReduxFirebaseProvider } from 'react-redux-firebase'; |
| 37 | +import { createFirestoreInstance } from 'redux-firestore'; |
| 38 | +import Home from './Home' |
| 39 | +import createStore from './createStore' |
| 40 | +import { firebase as fbConfig, reduxFirebase as rfConfig } from './config' |
| 41 | +import './App.css' |
23 | 42 |
|
| 43 | +// Initialize Firebase instance |
| 44 | +firebase.initializeApp(fbConfig) |
| 45 | + |
| 46 | +const initialState = window && window.__INITIAL_STATE__ // set initial state here |
| 47 | +const store = createStore(initialState) |
| 48 | + |
| 49 | +export default () => ( |
| 50 | + <Provider store={store}> |
| 51 | + <ReactReduxFirebaseProvider |
| 52 | + firebase={firebase} |
| 53 | + config={rfConfig} |
| 54 | + dispatch={store.dispatch} |
| 55 | + createFirestoreInstance={createFirestoreInstance}> |
| 56 | + <Home /> |
| 57 | + </ReactReduxFirebaseProvider> |
| 58 | + </Provider> |
| 59 | +) |
24 | 60 | ``` |
25 | 61 |
|
26 | 62 | ## Example Thunk |
27 | 63 |
|
28 | 64 | ```javascript |
29 | | -const sendNotification = (payload) => ({ |
30 | | - type: NOTIFICATION, |
31 | | - payload |
32 | | -}) |
33 | | - |
34 | | -export const addTodo = (newTodo) => |
35 | | - (dispatch, getState) => { |
36 | | - return firebase |
| 65 | +function sendNotification(payload) { |
| 66 | + return { |
| 67 | + type: NOTIFICATION, |
| 68 | + payload |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export function addTodo(newTodo) { |
| 73 | + return (dispatch, getState, getFirebase) => { |
| 74 | + return getFirebase() |
37 | 75 | .ref('todos') |
38 | 76 | .push(newTodo) |
39 | 77 | .then(() => { |
40 | 78 | dispatch(sendNotification('Todo Added')) |
41 | 79 | }) |
42 | | - }; |
43 | | - |
| 80 | +} |
44 | 81 | ``` |
0 commit comments