What is the point of the constants in redux?

后端 未结 3 919
旧巷少年郎
旧巷少年郎 2020-12-08 06:20

For example from this example:

export const ADD_TODO = \'ADD_TODO\'
export const DELETE_TODO = \'DELETE_TODO\'
export const EDIT_TODO = \'EDIT_TODO\'
export          


        
3条回答
  •  北海茫月
    2020-12-08 06:53

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

    Why is this beneficial? It is often claimed that constants are unnecessary, and for small projects, this might be correct. For larger projects, there are some benefits to defining action types as constants:

    • 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.

    Here's the link to the Github issue

提交回复
热议问题