Skip to content
Advertisement

How can I avoid Array-callback-return error in my code

I want to filter through a list of items and display the items according to my filtered term.

My list of items

JavaScript

I have a input element that is a search field

JavaScript

The search field has an onchange handler

JavaScript

I am rendering the list items using .map helper method

JavaScript

My state objects are as follows

JavaScript

I want the ability to render my list according to what I type in my <input/> element for search, so I did the following

JavaScript

After doing this, eslint gives me the following warning message
Array.prototype.filter() expects a value to be returned at the end of arrow function.eslintarray-callback-return


**Please help me understand this issue and how can I …**
  • Use another approach here, to do what I want with filtering and rendering my list
  • What is the problem with the way I’m doing it? (the code still works properly, but that warning message is annoying me)
  • What is a succinct way to do the same task

@Quentin I did the following refactor

JavaScript

Advertisement

Answer

What is the problem with the way I’m doing it?

ESLint wants you to explicitly return something: always.

If the code gets to the end of the function, it implicitly returns undefined. ESlint expects you to be explicit.

JavaScript

What is a succinct way to do the same task

JavaScript

… should do the trick.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement