小程序 redux

by Paul Matthew Jaworski

保罗·马修·贾沃斯基(Paul Matthew Jaworski)

Redux应用程序最重要的ESLint规则 (The Most Important ESLint Rule for Redux Applications)

tl;dr Run yarn add --dev eslint-plugin-import and add "import/named": 2 to your .eslintrc rules to prevent accidentally importing constants that don’t exist to your reducers.

tl; dr运行yarn add --dev eslint-plugin-import并将"import/named": 2到您的.eslintrc规则中,以防止意外导入在reducer中不存在的常量。

If you’re developing an application using React and Redux, your reducers probably look something like this:

如果您正在使用React和Redux开发应用程序,那么reducer可能看起来像这样:

This example is pretty straight-forward. You’re only importing one constant up top.

这个例子很简单。 您只需要导入一个常量即可。

Your file structure currently looks like this:

您的文件结构当前如下所示:

.├── actions|   ├── constants.js|   └── index.js...omitted for brevity...├── reducers|   ├── accountReducer.js|   └── index.js...omitted for brevity...├── indes.js└── index.html

But as your codebase grows, your reducers will become more complicated. Organizing your files by type may no longer makes sense. So you decide to start organizing things by feature or route instead:

但是随着代码库的增长,缩减器将变得更加复杂。 按类型组织文件可能不再有意义。 因此,您决定开始按功能或路线来组织事物:

.├── actions|   ├── constants.js|   └── index.js...omitted for brevity...├── reducers|   └── index.js├── routes|   ├── accounts|   |   ├── components|   |   ├── containers|   |   ├── module|   |   |   ├── actions.js|   |   |   ├── constants.js|   |   |   └── index.js (exports our reducer)|   |   ├── styles|   |   └── index.js|   └── index.js...omitted for brevity...├── indes.js└── index.html

Awesome! Now you don’t have 100 components in our main components folder anymore. Things are a bit neater, and easier to reason about.

太棒了! 现在您的main components文件夹中不再有100个组件。 事情变得更加整洁,并且更容易推理。

There’s a problem with your refactor, though. Suddenly this import in your reducer is now referring to a non-existent path:

但是,您的重构存在问题。 突然,在您的减速器中的此import现在引用的是不存在的路径:

import { RECEIVE_ACCOUNT_SUCCESS } from '../actions/constants';

You get an error about that path being unresolved immediately, so you change it:

您会收到有关该路径无法立即解决的错误,因此可以更改它:

import { RECEIVE_ACCOUNT_SUCCESS } from './constants';

Cool. But what if you didn’t actually define that constant in your new file?

凉。 但是,如果您实际上没有在新文件中定义该常量怎么办?

Now you’re about to experience one of the most frustrating bugs possible in a Redux app — importing an undefined constant into a reducer. Your tests will break, your app will break, and you’ll bash your head into your desk until you figure it out.

现在,您将体验Redux应用程序中最令人沮丧的错误之一-将未定义的常量导入reducer。 您的测试将失败,您的应用程序将失败,并且您将脑筋急转弯,直到弄清楚了。

The problem is that this type of bug just fails silently. ES6 imports don’t care whether or not the variable you’re importing is defined. You’ll never see an error about it.

问题是这种类型的错误只会以静默方式失败。 ES6导入不关心是否定义了要导入的变量。 您将永远不会看到有关它的错误。

ESLint进行救援! (ESLint to the Rescue!)

The key to avoiding this type of bug is to installing eslint-plugin-import, then set one simple little rule in your .eslintrc:

避免这种错误的关键是安装eslint-plugin-import ,然后在.eslintrc设置一个简单的小规则:

"import/named": 2

That’s it! Now you’ll get an error anytime you try to import an undefined constant into one of your reducers.

而已! 现在,当您尝试将未定义的常量导入其中的化简器中时,都会出现错误。

Edit: Unless you’re extending a base config that already includes it, you’ll also need to add "import" to the plugins section of your .eslintrc. Thanks to Dave Mac for pointing that out!

编辑:除非要扩展已经包含它的基本配置,否则还需要在.eslintrc的plugins部分中添加"import" 。 感谢Dave Mac指出这一点!

Happy coding!

编码愉快!

翻译自: https://www.freecodecamp.org/news/the-most-important-eslint-rule-for-redux-applications-c10f6aeff61d/

小程序 redux

小程序 redux_Redux应用程序最重要的ESLint规则相关推荐

  1. A - 小C语言--词法分析程序

    Description 小C语言文法 <程序>→<main关键字>(){<声明序列><语句序列>} <声明序列>→<声明序列>& ...

  2. 一个抓取电脑屏幕的小控件台程序

    一个抓取电脑屏幕的小控件台程序 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  3. Qt Creator开发基于小部件的应用程序

    Qt Creator开发基于小部件的应用程序 开发基于小部件的应用程序 代码编辑器集成 管理影像资源 指定Qt Designer的设置 使用设备皮肤预览表单 添加小部件 开发基于小部件的应用程序 Qt ...

  4. 三菱plc恒压供水程序+威纶触摸屏程序 以控制水泵一用一备、一拖二、一拖三、一拖四、一拖四带小泵恒压功能

    三菱plc恒压供水程序+威纶触摸屏程序本成已用于实际项目中 可以直接上载制成产品 也可用来学习plc恒压供水程序学习 喜欢可直接联系. 三菱PLC恒压供水程序说明 本程序采用三菱FX1N-30MR P ...

  5. java小项目家庭记账程序

    文章目录 java小项目家庭记账程序 **1.项目目标:** 2.项目需求: 3.具体思路: 4.具体代码: 1.页面: 2.Option_1类: 3.Option_2类: 4.Option_3类: ...

  6. 小C语言–词法分析程序

    小C语言–词法分析程序 Time Limit: 1000 ms Memory Limit: 5000 KiB Problem Description 小C语言文法 1. <程序>→(){& ...

  7. 小C语言--词法分析程序

    小C语言--词法分析程序 Time Limit: 1000 ms Memory Limit: 65535 KiB Submit Statistic Problem Description 小C语言文法 ...

  8. 【Exception】微信小程序,配置普通链接二维码规则 文件校验失败问题 校验文件检查失败 扫普通链接二维码打开小程序

    [Exception]微信小程序,配置普通链接二维码规则 文件校验失败问题 校验文件检查失败 扫普通链接二维码打开小程序 一.问题描述 1.公司业务有个需求,扫描普通的二维码,跳转到小程序的某个页面, ...

  9. 微软小娜打开应用程序_利用您现有的Microsoft投资进行应用程序白名单

    微软小娜打开应用程序 Application Whitelisting? What Is It? 应用白名单? 它是什么? I consider a firewall to be a Yes / No ...

最新文章

  1. python3 sys.stdout.write print 区别
  2. ios获取软键盘完成事件
  3. Outlook 2010 配置关联QQ邮箱
  4. PHP中插件机制的一种实现方案
  5. date设置时间_解决 IDEA 无法找到 java.util.Date 的问题
  6. Bootstrap-CSS-代码
  7. Bootstrap3 缩写词样式
  8. 093:QuerySet API详解-QuerySet转换为SQL的条件
  9. 【逆向工具】使用x64dbg+spy去除WinRAR5.40(64位)广告弹框
  10. 微信小程序申请 软件著作权(其它 软件,小程序都可以)
  11. 解决笔记本键盘禁用失败问题
  12. 随机过程 Class 3 条件期望
  13. 凸包(convex hull),凸包络面(convex envelope), 凸低估计量(convex underestimator), 图上方(epigraph),
  14. nginx修改主目录、主页
  15. 除了花瓣、站酷,提高品位、汲取灵感,关注这10个国内优质设计网站
  16. 用计算机可以定位到手机吗,如何使用计算机定位手机的位置?
  17. 自定义注解实现RBAC权限校验,不要再说你不会了
  18. 三菱不同型号plc之间能否实现无线通讯?
  19. 设备全生命周期管理,开启设管管理新模式
  20. 【Linux 用户和组】基础概念

热门文章

  1. css基础选择器 1204
  2. 根据注释生成项目文档
  3. Remix Solidity IDE 快速入门
  4. Splunk学习心得
  5. 爪哇国新游记之二十八----从url指定的地址下载文件到本地
  6. 张家口市12320卫生热线呼叫中心预计今年初启动
  7. win10配置gcc编译环境
  8. SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
  9. 713C - 如何进入一个研究领域
  10. 配置ISCSI客户端(LINUX)