zoukankan      html  css  js  c++  java
  • react 项目配置以及使用redux-saga

    1.配置store下的index.js文件

    import { createStore , applyMiddleware ,compose } from 'redux'  //  引入createStore方法
    import reducer from './reducer'    
    import saga from 'redux-saga'
    import mySagas from './sagas'
    
    const sagaMiddleware = saga();
    
    const composeEnhancers =   window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
        window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}):compose
    
    const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware))
    
    const store = createStore( reducer, enhancer) // 创建数据存储仓库
    sagaMiddleware.run(mySagas)
    
    export default store   //暴露出去

    2.组件中创建action

    componentDidMount(){
             const action = getMyListAction()
            store.dispatch(action)
        }

    3.actionCreators中写getMyListAction,getListAction 方法

    export const getListAction =(data)=>({
        type:GET_LIST,
        data
    })
    export const getMyListAction =()=>({//返回还是对象 saga
        type:GET_MY_LIST
    })

    4.配置sagas.js文件

    import { takeEvery, put } from 'redux-saga/effects'
    import { GET_MY_LIST } from './actionTypes'
    import { getListAction } from './actionCreators'
    import axios from 'axios'
    
    function* mySaga(){
        yield takeEvery(GET_MY_LIST,getList)
    }
    
    function* getList(){
         const res = yield axios.get('/api/getGoodsMenu')
         const action = getListAction(res.data.result)
         yield put(action)
    }
    export default mySaga;

    5.reducer文件

    case GET_LIST:
          newState.list=action.data;
          return newState;

    6.actionTypes

    export const GET_MY_LIST= 'getMyList'
  • 相关阅读:
    Python学习————并发编程
    Python学习————作业
    Python学习————网络编程
    Python学习————异常处理
    Python学习————反射
    Python学习————绑定方法
    Python学习————继承
    1765 谷歌的恐龙
    2504 是子序列的个数
    51Nod2386 分则能成
  • 原文地址:https://www.cnblogs.com/hllzww/p/13043069.html
Copyright © 2011-2022 走看看