I am following the redux tutorial https://redux.js.org/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?
They'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);