简体   繁体   中英

Unexpected block statement surrounding arrow body

This is likely trivial but I've spent an abnormal amount of time trying to figure out why I get this "Unexpected block statement surrounding arrow body " error for the code below

computed: {
  filteredItems() {
    return this.items.filter((item) => {
      return (item.type.toLowerCase().indexOf(this.search.toLowerCase()) > -1);
    });
  },
},

This is based on your ESLint configuration. So, since arrow function does implicit return, you don't need a return statement for your code.

The open braces after the arrow function immediately indicate a new block which should be more than a sentence but not so in your own case.

// Fix for your code

computed: {
  filteredItems() {
   return this.items.filter((item) => 
   (item.type.toLowerCase().indexOf(this.search.toLowerCase()) > -1));
  }
},

If in an arrow function all you do is return something then you can do the following:

const someFunction = item => item.someReturnValue;

So, in your case it is asking you to do the following:

return this.items.filter((item) => (item.type.toLowerCase().indexOf(this.search.toLowerCase()) > -1);

As you can see in my first example, you can also lose the brackets arounding the arguments if only one is required.

If you want to see some further examples then you can take a look at the MDN Arrow Functions page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM