zoukankan      html  css  js  c++  java
  • redux-thunk初步使用

    redux中间件,用来处理异步action

    返回 一个函数  内部函数接收存储方法dispatchgetState参数

    demo:

    import { GET_ONLINE_STATUS, SET_ONLINE_STATUS} from './action_type'
    import { changestatus, sysuserstatus } from '@/api'
    import { message } from 'antd';
    const getOnlineStatusAction = (val)=>{  //action
        return {
            type:GET_ONLINE_STATUS,
            playload:{
                text:val
            }
        }
    }
    
    export const getOnlineStatus = (params)=>{ 
        return (dispatch, getState)=>{              //返回 thunk 函数
            sysuserstatus(params).then(res=>{
                if(res.Ret === 200){
                    dispatch(getOnlineStatusAction(res.Status)) 
                }
            })
        }
    }
    
    
    
    //组建中使用
    const mapStateToProps = (state, ownProps) => ({
        status:state.handleOnlineStatus.onLineStatus //用户在线状态(state.handleOnlineStatus 此处这样用是因为reducer中使用了redux中的combineReducers()函数)
    })
    const mapDispatchToProps = {
        getOnlineStatus,
        setOnlineStatus
    }
    @connect(mapStateToProps,mapDispatchToProps)
    class{}
  • 相关阅读:
    192021
    191020
    magento注册
    magento登陆
    把PHP的数组变成带单引号的字符串
    magento直接操作数据库
    兼容各大浏览器的event获取
    手动修改magento域名
    微信支付中的jsapi返回提示信息
    CentOS 下安装xdebug
  • 原文地址:https://www.cnblogs.com/cuikaitong/p/11057227.html
Copyright © 2011-2022 走看看