我是react-redux的新手,我想知道以下两个在reducer中使用的代码片段之间的区别。
这是第一个代码片段:
[types.GET_TAGS]: (state, {payload}) => {
return {
...state,
tagData:null
};
},第二个代码片段如下:
[types.GET_TAGS]: (state, { payload }) => ({
...state,
tagData: null
}),在第一个代码片段中使用了return语句...有什么关系呢?
发布于 2020-05-13 14:18:03
这是没有区别的。当你想直接“返回”某些东西时,这是一条捷径。
一个普通函数的例子:
const test = () => {
return 'hello'
}由于我们什么也不做,我们只想直接返回'hello‘,所以我们可以编写一段更短的代码,即:
const test = () => 'hello'发布于 2020-05-13 14:15:03
没有区别,第二个是简短的符号,单词返回被省略了。
发布于 2020-05-13 14:17:40
这里没有与react-redux相关的东西。不同之处在于箭头函数的符号。
第二个代码是第一个代码的简短符号。
例如,
const a = () => {return 1};
a(); // will return 1类似地,
const b = () => (1);
b(); // will return 1https://stackoverflow.com/questions/61767578
复制相似问题