Redux中的命名约定

huangapple go评论72阅读模式
英文:

Naming conventions in redux

问题

在Redux中,有一些事情是必须记住的。为了创建一个切片(slice),关键字是"reducers",这是正确的,因为可以有多个reducer函数。要从切片中导出,我们使用"slice.reducer"。这里存在一些类型不匹配的问题,这是大多数人在使用之前需要记住的,因为没有明确的逻辑来解释为什么名称会这样。这会影响到学习Redux的过程。

我希望这个问题能够得到解决,这将帮助使用Redux的人理解他们在背后所做的事情。我自己制作了一个Redux包,几乎像官方的那样工作,并解决了这个问题。

英文:

There are some things in redux that one has to remember for using it.
For creating a slice the keyword is reducers which is right as there can be many reducer functions
For exporting from the slice we use "slice.reducer". There is some type of mismatch here, which one mostly needs to remember before using it, as it has no definite logic as to why the names are like this. This impacts in the learning curve of redux.

I want this problem to be solved, it will make the one who uses redux to understand what he is doing behind the scenes. I made my own redux package, which almost works like the official, and have solved the issue.

答案1

得分: 1

这不是不匹配的问题。

createSlice 接受多个(情况)reducers,并将它们全部放入一个reducer函数中。
如果你仔细看,slice.reducer 不是一个包含多个项(这对于复数是有道理的)的对象或数组 - 它是一个单数的函数。

一般来说:使用具有TypeScript支持的IDE,如Visual Studio Code。即使你只是编写纯JavaScript,编辑器也会使用库类型来告诉你可用的选项以及选项的命名方式。没有必要记住所有这些。

英文:

It is not a mismatch.

createSlice takes multiple (case) reducers and puts all of them into one reducer function.
If you look at it, slice.reducer is not an object or array with multiple items (which would make sense with a plural) - it's a singular function.

Generally though: use an IDE with TypeScript support like Visual Studio Code. Even if you are just writing plain JavaScript, the editor will use the library types to tell you what is available and how the options are named. No reason to remember all that.

huangapple
  • 本文由 发表于 2023年2月14日 04:51:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定