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

前端 未结 2 825
予麋鹿
予麋鹿 2020-12-24 09:36

I saw this code on a package:

const SortableList = SortableContainer(({items}) => {
 return (
     
    {items.map((value, index) =>
2条回答
  •  庸人自扰
    2020-12-24 09:56

    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
    

提交回复
热议问题