import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native'
import { Provider } from 'react-redux'
import store from './src/store'
class Counter extends Component {
render() {
return (
<Provider store={store}>
...
</Provider>
);
}
}
AppRegistry.registerComponent('Counter', () => Counter)
Middleware
為了適應異步(asynchronous)操作和其他自定義的操作,Redux提供了middleware結構。
ps.所謂的異步操作,ex.跟server要資料,call Api…等等
In Redux, middleware is injected between the dispatching of an action and its arrival at the reducer.
我們需要將 middleware 透過 applyMiddleware 加進store內。applyMiddleware為 redux 內的一個function
以下為sample code,加入 redux-logger 和 redux-thunk(記得要先install(yarn) redux-logger 和 redux-thunk)
import { createStore, combineReducers, applyMiddleware} from 'redux'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'
import reducers from './reducers'
const logger = createLogger()
const createStoreWithMiddleware = applyMiddleware(logger, thunk)(createStore)
const store = createStoreWithMiddleware(combineReducers(reducers))
export default store