简体   繁体   English

为什么我应该使用导出的常量设置redux动作类型?

[英]Why I should set redux actions type with an exported constants?

I'm trying to learn react with redux, I've read in the main documentation of redux, we should store actions types into one js files and then export all constants and use them into type, for example it's simple mode: 我正在尝试学习与redux的反应,我在redux的主要文档中读到,我们应该将动作类型存储到一个js文件中,然后导出所有常量并将它们用于类型,例如它的简单模式:

 export function itemsAction(items) { return { type: 'ITEMS', data: items } } 

But apparently I should do in this format: 但显然我应该采用这种格式:

 import { types } from './Types'; export function itemsAction(items) { return { type: types.ITEMS, data: items } } 

But what's the main benefit of this trick? 但这个伎俩的主要好处是什么? actions are fixed and never changes, why I should set the action types from variables and import all types ? 操作是固定的,永远不会改变,为什么我应该从变量设置动作类型并导入所有类型?

Thanks 谢谢

Because it's good to know you're using an object property rather than a raw string, since raw strings are prone to typos. 因为知道你正在使用对象属性而不是原始字符串是很好的,因为原始字符串容易出现拼写错误。 Take your example: 举个例子:

export function itemsAction(items) {
    return {
        type: 'ITEMS',
        data: items
    }
}

Now let's say I have a reducer to handle that action that also uses a raw string: 现在让我们假设我有一个reducer来处理也使用原始字符串的动作:

const todos = (state = [], action) => {
  switch (action.type) {
    case 'ITESM':
      return state.map(item => item.something = "potato")
    default:
      return state
  }
}

In the reducer, I misspelled the action type for its case. 在reducer中,我拼错了它的案例的动作类型。 But due to how Javascript and Redux work, it won't get noticed as an error in your IDE linting, nor will it produce any errors when you execute the code. 但是由于Javascript和Redux的工作方式,它不会在IDE linting中被注意为错误,也不会在执行代码时产生任何错误。 You'll dispatch the action, and it will silently fail. 你将调度该动作,它将无声地失败。 Nothing will happen, no action, no errors, nothing. 什么都不会发生,没有行动,没有错误,没有。 This can be a pain to debug. 这可能是一个痛苦的调试。

In addition, what if you want to change the name of an action. 此外,如果要更改操作的名称,该怎么办? It's good to just go change the value in the object, rather than do a big find and replace across your whole project, hoping you don't overwrite something you didn't mean to in the process. 最好只更改对象中的值,而不是在整个项目中进行大的查找和替换,希望您不要覆盖在此过程中您不想要的内容。

Plus it's just nice to have all your action types in one place, so you can go through and see what you have available, rather than looking through all your action files. 此外,将所有操作类型放在一个位置非常方便,因此您可以查看可用内容,而不是查看所有操作文件。 Once your project grows, you'll be glad you did it. 一旦你的项目成长,你会很高兴你做到了。

Also, on Typescript it's especially useful, since if you try to use an action you haven't defined it won't even build, so you get some build time safety. 此外,在Typescript上它特别有用,因为如果你尝试使用一个你没有定义的动作,它甚至都不会构建,所以你可以获得一些构建时间的安全性。

This may be a duplicate of this question . 这可能与此问题重复

My answer though 我的回答

Action types are going to be referred to several times in your code. 操作类型将在您的代码中多次引用。 For example, in your reducer, you may also refer to your action type: 例如,在您的reducer中,您还可以参考您的操作类型:

import {
  types
} from './actions'

...

function exampleReducer(state, action) {
  switch (action.type) {
    case types.ITEMS:
       // do something here
    default:
      return state
  }
}

Then you don't want to write type: 'ITEMS' again there. 然后你不想写type: 'ITEMS'再次出现。 You want to write types.ITEMS to avoid repeating yourself. 你想写类型types.ITEMS以避免重复自己。 It's always a good practice to refer to constant variables rather than writing them over again in plain strings. 引用常量变量而不是用普通字符串再次写入它们总是一个好习惯。

Types get especially hard to remember when you have multiples of them and usages in multiple files. 当你有多个文件和多个文件中的用法时,类型变得特别难以记住。 So it's really good for you to have action types as constants. 因此,将动作类型作为常量非常有用。

It's a good habit to see your action types in one file sorted out so that you can always have a look over it and change them as needed (as opposed to changing 'ITEMS' to 'ITEM' in your 4 files if you were to change the name of the action type). 在一个文件中查看您的操作类型是一个很好的习惯,这样您就可以随时查看它并根据需要进行更改(而不是将4个文件中的'ITEMS'更改为'ITEM' ,如果您要更改的话动作类型的名称)。

ETC 等等

You usually do something like 你经常这样做

import {
  ITEMS,
  ...more action types
} from './actions'

and refer to it directly (which is more concise): 并直接引用它(更简洁):

export function itemsAction(items) {
    return {
        type: ITEMS,
        data: items
    }
}

It's beneficial to keep actions in a separate file because it reduces the code duplication. 将操作保存在单独的文件中是有益的,因为它减少了代码重复。 Your actions names will be required in actions, reducers (possibly multiple reducers) and epics (if you use redux-observable). 动作,缩减器(可能是多个缩减器)和史诗(如果使用redux-observable)将需要您的动作名称。 Always copy-pasting action name string is error prone, you can easily end up with a typo. 始终复制粘贴操作名称字符串容易出错,您可以轻松地输入错字。

If you put action names in a separate file, you can be more confident about using them in the system and refactoring later since you have a single point of truth. 如果将动作名称放在单独的文件中,您可以更自信地在系统中使用它们并稍后重构,因为您有一个单一的事实真相。

If you decide to use TypeScript, try something like typesafe-actions . 如果您决定使用TypeScript,请尝试类似typesafe-actions的操作 TypeScript eliminates the need for having action names in a separate constants file since action name can become a real type. TypeScript消除了在单独的常量文件中使用操作名称的需要,因为操作名称可以成为真实类型。 This opens up a lot of things like having intellisense when writing actions in reducers and epics 这开启了许多诸如在减速器和史诗中编写动作时具有智能感知的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM