|
| 1 | +# Setting up Redux for React Native |
| 2 | + |
| 3 | +Let begin by installing few packages. |
| 4 | + |
| 5 | +`yarn add redux react-redux redux-promise redux-thunk` |
| 6 | + |
| 7 | + or |
| 8 | + |
| 9 | +`npm install --save redux react-redux redux-promise redux-thunk` |
| 10 | + |
| 11 | +- **redux** - the main redux library. |
| 12 | +- **react-redux** - the react bindings for redux, which makes our life easy when using redux with react. |
| 13 | +- **redux-promise** - promise middleware for Redux. |
| 14 | +- **redux-thunk** - Thunk middleware for redux (explained in detail in later chapter). |
| 15 | + |
| 16 | + |
| 17 | +Now create the files |
| 18 | +**`app/redux/store.js`** |
| 19 | +```js |
| 20 | + |
| 21 | +import {createStore, compose, applyMiddleware} from 'redux'; |
| 22 | +import thunk from 'redux-thunk'; |
| 23 | +import promise from 'redux-promise'; |
| 24 | +import rootReducer from './reducers/root.reducer'; |
| 25 | + |
| 26 | +const enhancerList = []; |
| 27 | +const devToolsExtension = window && window.devToolsExtension; |
| 28 | + |
| 29 | +if (typeof devToolsExtension === 'function') { |
| 30 | + enhancerList.push(devToolsExtension()); |
| 31 | +} |
| 32 | + |
| 33 | +const composedEnhancer = compose(applyMiddleware(thunk, promise), ...enhancerList); |
| 34 | + |
| 35 | +const initStore = () => createStore(rootReducer, {}, composedEnhancer); |
| 36 | + |
| 37 | +module.exports = { |
| 38 | + initStore |
| 39 | +}; |
| 40 | + |
| 41 | +``` |
| 42 | +**`app/redux/reducers/root.reducer.js`** |
| 43 | +```js |
| 44 | +import {combineReducers} from 'redux'; |
| 45 | + |
| 46 | +export default combineReducers({ |
| 47 | + |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +Now lets initialize the store. |
| 52 | + |
| 53 | +Modify the file: |
| 54 | +**`app/index.js`** |
| 55 | +```js |
| 56 | +import React, {Component} from 'react'; |
| 57 | +import {initStore} from './redux/store'; |
| 58 | +import {Provider} from 'react-redux'; |
| 59 | + |
| 60 | +import App from './App.container'; |
| 61 | + |
| 62 | +const store = initStore(); |
| 63 | + |
| 64 | +class NoteTaker extends Component { |
| 65 | + render () { |
| 66 | + return ( |
| 67 | + <Provider store={store}> |
| 68 | + <App /> |
| 69 | + </Provider> |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export default NoteTaker; |
| 75 | +``` |
| 76 | + |
| 77 | +and move the Initialization of the home component to another file |
| 78 | + |
| 79 | +**`app/App.container.js`** |
| 80 | +```js |
| 81 | +import React, {Component} from 'react'; |
| 82 | +import Home from './components/Home/Home.component'; |
| 83 | +import {connect} from 'react-redux'; |
| 84 | + |
| 85 | +class App extends Component { |
| 86 | + render () { |
| 87 | + return ( |
| 88 | + <Home /> |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const mapStateToProps = (state) => ({ |
| 94 | + state |
| 95 | +}); |
| 96 | + |
| 97 | +const mapDispatchToProps = (dispatch) => ({ |
| 98 | + dispatch |
| 99 | +}); |
| 100 | + |
| 101 | +export default connect(mapStateToProps, mapDispatchToProps)(App); |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +Now, lets add our first reducer and action. |
| 106 | + |
| 107 | + |
| 108 | +Create the files: |
| 109 | +**`app/redux/actions/index.actions.js`** |
| 110 | +This file will contain all our actions. |
| 111 | + |
| 112 | +```js |
| 113 | +export const TEST_ACTION = 'TEST_ACTION'; |
| 114 | +``` |
| 115 | + |
| 116 | +**`app/redux/reducers/test.reducer.js`** |
| 117 | +```js |
| 118 | +import {TEST_ACTION} from '../actions/index.actions'; |
| 119 | + |
| 120 | +const test = (state = {}, action) => { |
| 121 | + switch (action.type) { |
| 122 | + case TEST_ACTION: { |
| 123 | + return action.payload; |
| 124 | + } |
| 125 | + default: |
| 126 | + return state; |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +export default test; |
| 131 | +``` |
| 132 | +Now lets add our test reducer to the root reducer. |
| 133 | + |
| 134 | +Modify **`app/redux/reducers/root.reducer.js`** |
| 135 | + |
| 136 | +```js |
| 137 | +import {combineReducers} from 'redux'; |
| 138 | +import test from './test.reducer'; |
| 139 | + |
| 140 | +export default combineReducers({ |
| 141 | + test |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +At this point, we should a have a redux store with an initial test state from the test reducer. |
| 146 | + |
| 147 | +To check this, lets run our app on the simulator. |
| 148 | + |
| 149 | +Launch the react-native-debugger. |
| 150 | + |
| 151 | +Now open up the debug menu on the iOS simulator by pressing `cmd+ctrl+z` or on Android emulator by using `cmd+m`. |
| 152 | + |
| 153 | +Choose `Debug JS Remotely`. |
| 154 | + |
| 155 | +This should run the app js code in react-native-debugger and if all went well we should see something like this on the redux panel: |
| 156 | + |
| 157 | +<br> |
| 158 | +<div style="text-align:center"> |
| 159 | + <img src="/assets/images/9/9.1/9.1-redux-setup.png" style="width: 80%;display:inline-block;" hspace="20"> |
| 160 | +</div> |
| 161 | +<br> |
| 162 | + |
| 163 | +This implies that our redux store successfully initialized with the test reducer. |
| 164 | + |
| 165 | + |
| 166 | +NOTE: |
| 167 | + |
| 168 | +If your tests fail due to the error: |
| 169 | + |
| 170 | +`window not defined` |
| 171 | + |
| 172 | +then add a mock file |
| 173 | +**`__mocks__/react-native.js`** |
| 174 | + |
| 175 | +```js |
| 176 | +var rn = require('react-native'); |
| 177 | +global.window = global; |
| 178 | +module.exports = rn; |
| 179 | +``` |
| 180 | + |
| 181 | +This will initialize a dummy window variable when tests are run in node environment. |
0 commit comments