[英]React Redux reducers
I'm a newbie to react-redux and want to know the difference between the following two code snippets used within the reducer.我是 react-redux 的新手,想知道减速器中使用的以下两个代码片段之间的区别。
This is the first code snippet:这是第一个代码片段:
[types.GET_TAGS]: (state, {payload}) => {
return {
...state,
tagData:null
};
},
And the second code snippet is this:第二个代码片段是这样的:
[types.GET_TAGS]: (state, { payload }) => ({
...state,
tagData: null
}),
In the first code snippet return
statement has been used... What's the difference?在第一个代码片段中已经使用了return
语句......有什么区别?
No difference, second is short notation, word return is omitted.没有区别,第二个是简写,回车字省略。
There's nothing related to react-redux out here.这里没有与 react-redux 相关的内容。 The difference is in the notation of the arrow function.不同之处在于箭头 function 的符号。
The second code is a short notation of the first one.第二个代码是第一个代码的简写。
For eg,例如,
const a = () => {return 1};
a(); // will return 1
Similarly,相似地,
const b = () => (1);
b(); // will return 1
There are no difference.没有区别。 It's a shortcut when you want to "return" something directly.当您想直接“返回”某些东西时,这是一个捷径。
Example on a normal function:普通 function 的示例:
const test = () => {
return 'hello'
}
since we're not doing anything and we just want to return the 'hello' directly, we can do a shorter code which is:因为我们什么都不做,只是想直接返回'hello',我们可以做一个更短的代码:
const test = () => 'hello'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.