十月八号是什么星座| 粉色裤子搭什么上衣| 应收账款在贷方表示什么| 调理是什么意思| 瑄字五行属什么| 什么银行卡最好用| 什么是sku| 婊子代表什么生肖| 小受是什么意思| 脂肪燃烧是什么感觉| 身上老出汗是什么原因引起的| 头出汗多至头发湿透是什么原因| 什么虫子咬了像针扎一样疼| 上火喉咙痛吃什么药| 海扶治疗是什么| 摸摸头是什么意思| 蜘蛛的血液是什么颜色| 10月26是什么星座| 痔疮什么感觉| 阴历六月十五是什么日子| 腺样体肥大有什么症状| 尿血是什么原因引起的| 规格型号是什么意思| 后话是什么意思| 孩子脾虚内热大便干吃什么药| 葡萄什么时候传入中国| 睾丸是什么东西| 农历5月是什么月| 血糖什么时候最高| 腺病毒是什么病毒| 驻唱是什么意思| 郭靖黄蓉是什么电视剧| 跨性别是什么意思| affairs是什么意思| 新鲜的乌梅长什么样| toryburch什么牌子| cno什么意思| 红细胞偏低是什么意思| 孟母三迁的故事告诉我们什么道理| 下身痒是什么原因| 知了猴什么时候出来| 郡肝是什么| 怀孕什么时候可以同房| 唐筛是检查什么| 黑豆有什么功效和作用| 眼睛长麦粒肿用什么药| 经常感冒吃什么增强抵抗力| 桃代表什么生肖| 口腔溃疡吃什么药好得快| 家是什么结构的字| 胃ct能检查出什么病| 九牛一毛是什么意思| 五行缺什么怎么查询| 寸头是什么意思| 什么是红眼病| 疱疹是什么病啊| 什么叫自私的人| pr是什么职位| 胃反酸吃什么食物好| 素海参是什么做的| 易经和周易有什么区别| 6月18什么星座| 眼睛发涩是什么原因导致的| 缺陷是什么意思| 女性的排卵期是什么时候| 头疼做什么检查| 托班是什么意思| 车前草长什么样| classic是什么意思| 旧衣服属于什么垃圾| ct和拍片有什么区别| tct检查什么项目| 摧残是什么意思| 什么人不能吃洋葱| 抖m是什么意思| 子宫肌层回声欠均匀是什么意思| 减肥期间可以吃什么零食| 减肥早餐吃什么好| 审美疲劳是什么意思| 天秤座男生喜欢什么样的女生| 海参是补什么的| 钨砂是什么东西| 青蟹什么季节吃最好| 神经官能症有什么症状表现| 腹泻拉水是什么原因| 脑白质疏松是什么病| pet-ct检查主要检查什么| 小腿发痒是什么原因| 胃动力不足是什么原因造成的| 第六感是什么| 农历五月是什么星座| 殆什么意思| 经期吃什么水果| 梦见跟别人打架是什么意思| 腰间盘膨出吃什么药效果好| 地龙是什么| 明天有什么考试| 欲言又止下一句是什么| 肺炎吃什么水果| 无回声结节是什么意思| 甲状腺结节有什么感觉| 肠易激综合征中医叫什么| 肝弥漫性病变是什么意思| 市公安局政委是什么级别| 做脑ct对人体有什么危害| 19岁属什么生肖| 人肉什么味道| 做放疗的人吃什么好| 女人吃鹅蛋有什么好处| 湿气重挂什么科| 头痛眼睛痛什么原因引起的| 白垩纪是什么意思| 补气血吃什么水果| 六月十一号是什么星座| 胆囊壁增厚是什么意思| 大便有点绿色是什么原因| 半夜喉咙痒咳嗽是什么原因| 自制力是什么意思| 分辨率dpi是什么意思| 活菩萨是什么意思| 肝有问题会出现什么症状| 时蔬是什么意思| 浮肿是什么原因引起的| 1989年属什么生肖| 何必是什么意思| 一片哗然是什么意思| hpv都有什么症状| 晴雨表是什么意思| bcr是什么意思| 间断是什么意思| 山竹为什么这么贵| 老年痴呆症挂什么科| 搬家送什么水果| 洗冷水澡有什么好处| 办理公证需要什么材料| 大腿前侧是什么经络| 溺爱的意思是什么| 广东省省长是什么级别| 脚趾发紫是什么原因| 初字五行属什么| 吃了避孕药有什么副作用| 禁忌什么意思| 什么木头做菜板好| 黄皮果什么味道| 关节发黑是什么原因| 爽肤水是什么| 湿热内蕴吃什么中成药| 唐卡是什么材料做的| 2020年是什么生肖| 做梦梦到老公出轨代表什么预兆| 半夜12点是什么时辰| 辽宁古代叫什么| 九月三号是什么日子| 做梦梦到男朋友出轨了是什么意思| 多西他赛是什么药| 二院是什么医院| 风向标是什么意思| 脂肪垫是什么| 什么颜色可以调成紫色| 劫财是什么意思| 督察是什么意思| 身上长扁平疣是什么原因造成的| 考教师资格证需要什么条件| 水手是干什么的| 舌头尖有小红点这是什么症状| 海鲜有什么| 什么是黄体期| 左肺结节是什么意思| 梦见自己掉牙是什么意思| jdk是什么| 动土是什么意思| 什么是阴道| 脖子上有痣代表什么| 诱导是什么意思| 有龙则灵的灵是什么意思| 无菌敷贴是干什么用的| 干涉是什么意思| 胃胀胃不消化吃什么药| 桃花什么生肖| study是什么意思| 1966年属什么今年多大| 经常吃维生素c有什么好处和坏处| hpvhr阳性什么意思| 孙悟空姓什么| 什么是袖珍人| 细胞学检查是什么| 鲜为人知是什么意思| 豆腐炖什么好吃| 你喜欢我什么我改| 虾肚子上的黑线是什么| 趋利避害是什么意思| 什么是爱呢| 孕前检查什么时候去最合适| 自媒体是什么| 瑜五行属什么| 猫咪能吃什么水果| 小肠与什么相表里| 不寐病是什么意思| 杜冷丁是什么药| 外阴瘙痒什么原因引起| 伤寒是什么意思| 舌苔黄腻吃什么中成药| 教头菜有什么功效| 肚脐连着什么器官| 什么名字最好听| guess是什么品牌| eagle是什么牌子| 狗狗哭了代表什么预兆| 三昧什么意思| 什么得什么造句| 萎靡是什么意思| 绿豆和什么相克中毒| 木全读什么| 脑梗用什么药效果好| 幽门螺杆菌吃什么药好| 小便黄是什么原因引起的| 肝脾不和吃什么中成药| 热狗为什么叫热狗| 下午3点到4点是什么时辰| 净身出户是什么意思| 扁平苔藓有什么症状| 头疼是因为什么| 什么洗面奶好用| 奋不顾身的顾是什么意思| 琉璃是什么材料| 小叶增生吃什么药好| 干什么最挣钱| 英国为什么叫日不落帝国| 375是什么意思| 血热吃什么药快速见效| 肾精亏虚吃什么中成药| 为什么左眼皮一直跳| 为什么体检前不能喝水| 什么是善| 阴道里面痒是什么原因| 雀神是什么意思| 为什么声音老是嘶哑| 仓鼠是什么动物| ahc是韩国什么档次| apc是什么牌子| 头发粗硬是什么原因| 牛肉不能和什么一起吃| 言尽于此是什么意思| tg医学上是什么意思| 什么书在书店买不到| 59岁属什么生肖| 阴道长什么样子| 不粘锅涂层是什么材料| 戒指上的s925是什么意思| 八拜之交是什么生肖| mpa什么意思| 罗森是什么| 长期吃阿司匹林有什么副作用| 水钠潴留什么意思| 眼睛模糊吃什么好| 88年的属什么生肖| 舌苔发黑是什么原因引起的| 反流性咽喉炎吃什么药| 手上脱皮是什么原因| 为什么鼻子无缘无故流鼻血| 莱赛尔是什么面料| 脸上长痣是什么原因| 失眠缺什么维生素| 上眼皮浮肿是什么原因| 百度

Idiomatic Redux: Why use action creators?

This is a post in the Idiomatic Redux series.


First in an occasional series of thoughts on good usage patterns for Redux

Preface ??︎

I've spent a lot of time discussing Redux usage patterns online, whether it be helping answer questions from learners in the Reactiflux channels, debating possible changes to the Redux library APIs on Github, or discussing various aspects of Redux in comment threads on Reddit and HN. Over time, I've developed my own opinions about what constitutes good, idiomatic Redux code, and I'd like to start sharing some of those thoughts. Despite my status as a Redux maintainer, these are just opinions, but I think they're pretty good approaches to follow :)

Action Creators ??︎

One of the most common complaints about Redux is the amount of "boilerplate" involved. People complain about having to declare constants, writing indirect code, touching multiple files to implement features, and so on. I've specifically seen people asking why "action creators" are needed in several places (such as here, here, and here).

So, why not just put all your logic right into a component? Dan Abramov has written some fantastic answers on similar topics, and I've answered this question several times, but I'd like to cover the topic in more detail.

First, let's start with some clarification of terms and concepts:

  • An action is a plain simple object, like {type : "ADD_TODO", text : "Buy milk"}.
  • An action type is the value for the type field in an action. Per the Redux FAQ, this field should be a string, although Redux only enforces that a type field exists in the action.
  • An action creator is a function that returns an action, like:
function addTodo(text) {
    return {
        type : "ADD_TODO",
        text
    }
}
  • A thunk action creator is a function that returns a function. If you're using the redux-thunk middleware, the inner function will be called and given references to dispatch and getState, like this:
function makeAjaxCall(someValue) {
    return (dispatch, getState) => {
        dispatch({type : "REQUEST_STARTED"});
        
        myAjaxLib.post("/someEndpoint", {data : someValue})
            .then(response => dispatch({type : "REQUEST_SUCCEEDED", payload : response})
            .catch(error => dispatch({type : "REQUEST_FAILED", error : error});    
    };
}

Reasons for Using Action Creators ??︎

Per Dan's answers on Stack Overflow, it's completely possible to do the work of making AJAX calls and calling dispatch entirely inline in a component. However, as programmers, it is good practice to encapsulate behavior, separate concerns, and keep code duplication to a minimum. We'd also like to keep our code as testable as possible.

To me, there are five primary reasons to use action creators rather than putting all your logic directly into a component:

  1. Basic abstraction: Rather than writing action type strings in every component that needs to create the same type of action, put the logic for creating that action in one place.
  2. Documentation: The parameters of the function act as a guide for what data is needed to go into the action.
  3. Brevity and DRY: There could be some larger logic that goes into preparing the action object, rather than just immediately returning it.
  4. Encapsulation and consistency: Consistently using action creators means that a component doesn't have to know any of the details of creating and dispatching the action, and whether it's a simple "return the action object" function or a complex thunk function with numerous async calls. It just calls this.props.someBoundActionCreator(arg1, arg2), and lets the action creator worry about how to handle things.
  5. Testability and flexibility: if a component only ever calls a function passed in as a prop rather than explicitly referencing dispatch, it becomes easy to write tests for the component that pass in a mock version of the function instead. It also enables reusing the component in another situation, or even with something other than Redux.

Dispatching Actions from Components ??︎

There's a number of semantically equivalent ways to dispatch Redux actions to the store from a component. For example, all these do the same thing:

// approach 1: define action object in the component
this.props.dispatch({
    type : "EDIT_ITEM_ATTRIBUTES", 
    payload : {
        item : {itemID, itemType},
        newAttributes : newValue,
    }
});

// approach 2: use an action creator function
const actionObject = editItemAttributes(itemID, itemType, newAttributes);
this.props.dispatch(actionObject);

// approach 3: directly pass result of action creator to dispatch
this.props.dispatch(editItemAttributes(itemID, itemType, newAttributes));

// approach 4: pre-bind action creator to automatically call dispatch
const boundEditItemAttributes = bindActionCreators(editItemAttributes, dispatch);
boundEditItemAttributes(itemID, itemType, newAttributes);

To me, the simplest and most idiomatic approach is to always use pre-bound action creators in your components. This removes the need for components to worry about dispatch, and makes them agnostic to Redux itself. In addition, I highly encourage the use of the object literal shorthand for binding actions with connect:

import {connect} from "react-redux";
import {action1, action2} from "myActions";


const MyComponent = (props) => (
    <div>
        <button onClick={props.action1}>Do first action</button>
        <button onClick={props.action2}>Do second action</button>
    </div>
)

// Passing an object full of actions will automatically run each action 
// through the bindActionCreators utility, and turn them into props

export default connect(null, {action1, action2})(MyComponent);

For a while, I used a utility function that could generate my mapDispatch functions, and ensure that all my bound action creators were available under a prop named actions (such as this.props.actions.someCallback()). However, I've changed my mind on that approach, and am phasing that out of my codebase as I go. I liked the explicitness of the intent, but eventually decided that it was too much of a conceptual coupling to Redux. My components had to "know" whether they were connected or not by knowing whether to call this.props.actions.someCallback() or this.props.someCallback(). It also introduced a bit of extra complexity and inconsistency.

Final thoughts ??︎

I know that many people dislike adding extra layers of indirection and abstraction, to which I sympathize. I've always been a very pragmatic, direct, "let's write the code that actually does this" kind of developer, and I hate HammerFactories and enterprise architectures where everything is configured with a 15,000-line XML file. I've also seen concerns that prolific use of Redux reduces React to nothing more than a "fancy templating language", or a reinvention of MVC under another name. But, to me, consistent use of action creators promotes readable code with a reasonable level of abstraction, is in keeping with software engineering principles of encapsulation and DRY, and provides just enough decoupling between "display logic" and "business logic" to keep a codebase manageable and flexible.

Further Information ??︎


This is a post in the Idiomatic Redux series. Other posts in this series:

星月菩提是什么 英国全称是什么 奥氮平片是什么药 医托是什么意思 胆量是什么意思
脑疝是什么原因引起的 腰椎间盘突出吃什么好 鹊桥是什么意思 西地那非有什么副作用 养生馆起什么名字好
多什么多什么 刘三姐是什么生肖 什么的夏天 每次来月经都会痛经什么原因 卧是什么意思
社会很单纯复杂的是人是什么歌 晚饭后散步有什么好处 c反应蛋白什么意思 碎银子是什么茶 放是什么偏旁
柚子是什么季节的水果hcv8jop2ns7r.cn 翕什么意思hcv9jop2ns2r.cn 邋遢什么意思hcv8jop5ns7r.cn 胃息肉是什么引起的jasonfriends.com 田野是什么意思hcv8jop0ns9r.cn
胃热口干口苦口臭吃什么药好hcv9jop7ns5r.cn 丧偶什么意思hcv9jop5ns4r.cn 牙齿根部发黑是什么原因hcv9jop4ns4r.cn 忽什么忽什么hcv8jop5ns8r.cn 梦到老公出轨是什么意思travellingsim.com
为什么会长水泡hcv8jop7ns9r.cn 吃什么对肺部好hcv9jop5ns8r.cn 甜菜什么意思hcv9jop0ns8r.cn 急性会厌炎吃什么药hcv7jop9ns2r.cn lv什么牌子hcv9jop7ns5r.cn
肺部ct应该挂什么科hcv9jop8ns2r.cn 哺乳期感冒了能吃什么药hcv8jop5ns2r.cn 哮喘用什么药ff14chat.com 人乳头瘤病毒是什么bjhyzcsm.com 脸上起疙瘩是什么原因hcv9jop8ns2r.cn
百度