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'
  • 相关阅读:
    P3916 图的遍历 dfs
    P4568 [JLOI2011]飞行路线 分层图最短路
    P1948 [USACO08JAN]电话线Telephone Lines spfa 二分答案
    P1849 [USACO12MAR]拖拉机Tractor bfs
    P1730 最小密度路径 floyed
    P1661 扩散 二分答案 并查集
    使用unittest和Django搭配写一个接口测试平台
    AJAX解决跨域的几种方式
    Django
    基于pytest框架自动化测试脚本的编写
  • 原文地址:https://www.cnblogs.com/hllzww/p/13043069.html
Copyright © 2011-2022 走看看