Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do curly braces around a variable in a function parameter mean

I saw this code on a package:

const SortableList = SortableContainer(({items}) => {
 return (
     <ul>
        {items.map((value, index) =>
            <SortableItem key={`item-${index}`} index={index} value={value} />
        )}
    </ul>
 );
});

What is happening to items by putting curly braces around it in the function parameters?

like image 883
Toli Avatar asked Dec 16 '16 19:12

Toli


2 Answers

This is destructuring assignment syntax.

As another example, the following two lines of code are equal:

const { items } = args

const items = args.items

Simply put, it is a simplified way of accessing specific field of a given variable for further use in that scope.

In your original example, it is declaring a variable items for use in the function body that is the items field of that first argument.

const SortableList = SortableContainer(({items}) => {
    // do stuff with items here

is equal to

const SortableList = SortableContainer((input) => {
    const items = input.items
    // do stuff with items here
like image 151
lawls544 Avatar answered Sep 20 '22 22:09

lawls544


This question is likely a repost: What do {curly braces} around javascript variable name mean

But as an answer, it's destructuring assignment. If your object being passed in mirrors the variable being referenced, you can retrieve that specific field during assignment.

like image 39
Blake Geraci Avatar answered Sep 23 '22 22:09

Blake Geraci