优化模块 优化redux的使用过程react
npm install redux react-redux
经过react-redux 提供的provider提供器 将全局状态对象挂载到根元素的上下文上npm
import Provider from 'react-redux'
import store from './store/store.js'
...
<Provider store = {store}>
<App></App>
</Provider>
...
在组件中使用全局状态值经过react-redux 提供的connect 从跟组件的上下文上获取store对象,经过高阶组件将得到到的store传给目标组件的propsredux
import {connect} from 'react-redux'
class Son1 ... {}
connect(state=>state)(Son1)
组件中修改全局状态值 不须要写组件监听组件 -> actionCreator -> redcuer ->组件(监听更新)promise
组件的里的监听不用写了 网络
actionCreator 默认作两个事情1.建立action 2.经过dispath 发送action actionCreator 使用插件以后只要 建立action app
在组件里发送异步
解决方式经过异步中间件 redux-thunk redux-sage redux-promise ....ide
安装异步插件 npm install redux-thunk 函数
在store.js 引入并使用优化
import {createStore,applyMiddleware} from 'redux'
import thunk from 'redux-thunk' //解决异步的插件
import reducer from './reducer'
export default createStore(reducer,applyMiddleware(thunk))
除了actionCreatore 须要修改以外 其余的都不用动 a. 若是是同步的方法也不须要动 返回actionb. 若是action里有异步 返回一个函数 函数能接受dispath 在函数内部 作网络请求 成功以后经过dispath手动的发送