close

How do I fix “Expected to return a value at the end of arrow function” warning?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I fix “Expected to return a value at the end of arrow function” warning? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I fix “Expected to return a value at the end of arrow function” warning ?

  1. How do I fix “Expected to return a value at the end of arrow function” warning ?

    do I fix “Expected to return a value at the end of arrow function” warning A map() creates an array, so a return is expected for all code paths (if/elses).
    If you don't want an array or to return data, use forEach instead.

  2. How do I fix “Expected to return a value at the end of arrow function” warning ?

    do I fix “Expected to return a value at the end of arrow function” warning A map() creates an array, so a return is expected for all code paths (if/elses).
    If you don't want an array or to return data, use forEach instead.

Method 1

The warning indicates that you’re not returning something at the end of your map arrow function in every case.

A better approach to what you’re trying to accomplish is first using a .filter and then a .map, like this:

this.props.comments
  .filter(commentReply => commentReply.replyTo === comment.id)
  .map((commentReply, idx) => <CommentItem key={idx} className="SubComment"/>);

Method 2

map() creates an array, so a return is expected for all code paths (if/elses).

If you don’t want an array or to return data, use forEach instead.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment