4

What do the braces mean in the following function definition?

(state, { copy }) => state.push(createItem(copy));
SACRD
  • 63
  • 4

1 Answers1

6

In ES6, this is known as Object Destructuring.

In that particular aspect, it means I can pass an object in as a parameter of a function and it gets transformed into variables that I can access within that function, for example:

function f(param1, { param2, param3 }) {
  console.log(param2, param3)
}
f("one", {
  param2: "A",
  param3: "B"
});

Babel REPL Example

The advantage being that instead of calling:

f("one", myObject.one, myObject.two);

I can simply call:

f("one", myObject);

And the one, two parameters will be mapped for me accordingly.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176