zoukankan      html  css  js  c++  java
  • bindActionCreators简要概述

    bindActionCreators是redux的一个自带函数,作用是将单个或多个ActionCreator转化为dispatch(action)的函数集合形式。

    开发者不用再手动dispatch(actionCreator(type)),而是可以直接调用方法。

    可以实现简化书写,减轻开发的负担。

    例如:

    return {
            // 当触发addNews时,dispatch会执行异步action
            addNews(){
                dispatch(async (a)=>{
                    // console.log(a == dispatch);// false dispatch
                    await axios.post("http://127.0.0.1/news",{
                        newsTitle:this.refs.newsTitle.value,
                        newsHref:this.refs.newsHref.value
                    });
                    this.props.getNews();
                })
            },
            getNews(){
                dispatch(async (b)=>{
                    console.log(b)
                    // 获取新闻列表,并将新闻列表放到仓库状态中。
                    const {data} = await axios.get("http://127.0.0.1/news");
                    // dispatch(changeNewsList(data.newsList))
                    dispatch(changeNewsList(data.newsList))
                })
            }
        }
    利用bindActionCreators:
     return {
            newsList:state.news.newsList
        }
    }
    // 操作状态。
    function mapDispatchToProps(dispatch) {
    
        return bindActionCreators(newsCreatore,dispatch);
    }
    通过actions对象调用方法,就可以dispatch所有的action
  • 相关阅读:
    robotframework-ride1.7.3.1更新安装
    批量删除新浪微博
    Redis
    GET和POST两种基本请求方法的区别
    selenium2自动化测试实战--基于Python语言
    同步/异步/阻塞/非阻塞/BIO/NIO/AIO
    HTTP抓包实战
    LCT模板(BZOJ2631)
    树链剖分模板(BZOJ3083)
    凸包(BZOJ1069)
  • 原文地址:https://www.cnblogs.com/itgezhu/p/13724296.html
Copyright © 2011-2022 走看看