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

查看:57
本文介绍了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.

如果在导入动作常量时输入错误,将无法定义.当您想知道为什么在分派动作时什么也没发生时,这比错字更容易注意到.

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天全站免登陆