redux 中的常量有什么意义? [英] What is the point of the constants in redux?

查看:23
本文介绍了redux 中的常量有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如来自这个例子:

export const ADD_TODO = 'ADD_TODO'
export const DELETE_TODO = 'DELETE_TODO'
export const EDIT_TODO = 'EDIT_TODO'
export const COMPLETE_TODO = 'COMPLETE_TODO'
export const COMPLETE_ALL = 'COMPLETE_ALL'
export const CLEAR_COMPLETED = 'CLEAR_COMPLETED'

这不像是在保存字符.变量名与字符串完全相同,永远不会改变.如果有一天你要做这样的事情,我理解制作常量:

It's not like you're saving characters. The variable name is exactly the same as the string, and will never change. I understand making constants if one day you were doing to do something like:

ADD_TODO = 'CREATE_TODO'

但这从来没有发生过.那么这些常量的作用是什么?

but that never happens. So what purpose do these constants serve?

推荐答案

我想引用 @dan_abramov,Redux 的作者,来自对 Github 类似问题的评论.

I would like to quote @dan_abramov, the author of Redux from a comment on similar Github issue.

为什么这是有益的?人们经常声称常量是不必要的,对于小型项目,这可能是正确的.对于较大的项目,将操作类型定义为常量有一些好处:

  • 这有助于保持命名一致,因为所有操作类型都聚集在一个地方.

  • It helps keep the naming consistent because all action types are gathered in a single place.

有时您希望在处理新功能之前查看所有现有操作.可能您需要的操作已由团队中的某个人添加,但您不知道.

Sometimes you want to see all existing actions before working on a new feature. It may be that the action you need was already added by somebody on the team, but you didn’t know.

在拉取请求中添加、删除和更改的操作类型列表可帮助团队中的每个人跟踪新功能的范围和实现.

The list of action types that were added, removed, and changed in a Pull Request helps everyone on the team keep track of scope and implementation of new features.

如果你在导入动作常量时打错了字,你会得到 undefined.当您想知道为什么在调度 action 时什么也没有发生时,这比打字错误更容易注意到.

If you make a typo when importing an action constant, you will get undefined. This is much easier to notice than a typo when you wonder why nothing happens when the action is dispatched.

这是 Github 问题的链接

这篇关于redux 中的常量有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆