I am following the redux tutorial and having a hard time understanding the following code
export function addTodo(text) {
return { type: ADD_TODO, text }
}
So the function above returns the object { type: ADD_TODO, text }
and there are two things that confuse me.
what is the value of this object associated with the key
text
. If this value is undefined, then why not just return{ type: ADD_TODO}
instead.If
text
is a string, then shouldn't it be{ type: ADD_TODO, [text]: *some value* }
instead?
Moreover, there are other functions like
function toggleTodo(index) {
return { type: TOGGLE_TODO, index }
}
function setVisibilityFilter(filter) {
return { type: SET_VISIBILITY_FILTER, filter }
}
Can someone explain this syntax to me?
I am following the redux tutorial https://redux.js/basics/actions and having a hard time understanding the following code
export function addTodo(text) {
return { type: ADD_TODO, text }
}
So the function above returns the object { type: ADD_TODO, text }
and there are two things that confuse me.
what is the value of this object associated with the key
text
. If this value is undefined, then why not just return{ type: ADD_TODO}
instead.If
text
is a string, then shouldn't it be{ type: ADD_TODO, [text]: *some value* }
instead?
Moreover, there are other functions like
function toggleTodo(index) {
return { type: TOGGLE_TODO, index }
}
function setVisibilityFilter(filter) {
return { type: SET_VISIBILITY_FILTER, filter }
}
Can someone explain this syntax to me?
Share Improve this question asked Dec 24, 2019 at 2:37 PhantomPhantom 4431 gold badge6 silver badges14 bronze badges 1-
{ type: ADD_TODO, text }
is the es6 shorthand for{ type: ADD_TODO, text:text }
given a variabletext
. – Mark Commented Dec 24, 2019 at 2:39
1 Answer
Reset to default 8They're using ES6 Shorthand property names - If the intended key name is the same as the variable, then you can simply pass the variable
let name = 'Jared';
let age = 19;
let literate = false;
let obj = {
name,
age,
literate
}
/* Is the same as...
let obj = {
'name': name,
'age': age,
'literate': literate
}
*/
console.log(obj);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744783584a4593487.html
评论列表(0条)