Problem

The error "Expected a return value in arrow function" occurs when we write a javascript (JS) map method which does not have a return value.

Error

For example, In a React application, the "map" is often used to render components as the return value. Like in this example using parentheses for indicating a direct return call.

previewPosts?.map((post, i) => (
 	<PostCard key={post.id} {...{post, num: i }} />
))

or like this using brackets with an explicit return call.

previewPosts?.map((post, i) => {
   	return (
  		<PostCard key={post.id} {...{post, num: i }} />
    )
})

If you are not returning anything using either of these techniques, then the "Expected a return value in arrow function" error will be thrown.

Solution

If your intention is just to iterate over a list of items and perform some task without any return value, use the "forEach" method instead. This serves the purpose of iterating over a list and does not require a return value. For example,

let csvData = []
previewPosts?.forEach((item) => {
	csvData.push({
		"Name": item?.name,
		"Description":  item?.desc,
	})
})

Thank you for visiting my blog, feel free to explore more posts featured on my blog.