What is the point of the constants in redux?

后端 未结 3 915
旧巷少年郎
旧巷少年郎 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:49

    This is more useful in other languages, but also somewhat useful in JavaScript. For instance, if I used "ADD_TODO" throughout the code, instead of ADD_TODO, then if I make a mistake typing any of the strings, if it was code like, if (action === 'ADD_TODOz'), when that code executes, it will do the wrong thing. But if you mistyped the name of the const, if (action === ADD_TODOz), you'll get som kind of a ReferenceError: ADD_TODOz is not defined when that line attempts to execute -- because ADD_TODOz is an invalid reference. And, of course, in a static language, you'll get an error at "compile time."

提交回复
热议问题