0

Something doesn't seem right here. it doesn't render the Animated.View without using return, while I've seen this work in many examples. Any guesses why this behaviour?

enter image description here

BradByte
  • 11,015
  • 2
  • 37
  • 41
scazzy
  • 950
  • 2
  • 8
  • 20
  • 1
    map is just a function. If it doesn't return you anything, you cannot have any value to operate on. It's generic behaviour of map – Bikas Dec 08 '16 at 12:30
  • 1
    Look at the braces in your arrow functions. `foo => bar` or `foo => ( bar )`: implicit return, `foo => { bar }` needs explicit return. – pawel Dec 09 '16 at 10:19
  • @Bikas Thank you for that. I cannot tell you how long I was facing a lack of the views inside map() displaying. Your short statement brought me to a face palm moment, but it all works now. – Alec Bennett Nov 18 '17 at 01:21

5 Answers5

4

I know there's been several answers already, but I believe they fail to address OP specific question of why it is doesn't work, especially given the new ES6 syntax.

tl;dr: ES6 arrow functions with block bodies do not implicity return.

From the docs:

Arrow functions can have either a "concise body" or the usual "block body".

In a concise body, only an expression is needed, and an implicit return is attached. In a block body, you must use an explicit return statement.

This is an example of "concise body", using the filter prototype:

let myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let filteredArray = myArray.filter(item => item > 5);
console.log(filteredArray);
// => [6, 7, 8, 9, 10] 

In this expression, the lack of brackets { } mean that it will implicitly return the result of the expression.

The "block body" equivalent would be:

let myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let filteredArray = myArray.filter(item => {
  return item > 5;
});
console.log(filteredArray);
// => [6, 7, 8, 9, 10] 

In your post, you are specifying your component inside the block body without returning it, and since the map prototype requires that you return the item inside the callback that will be used for the new map, the map is empty.

Community
  • 1
  • 1
BradByte
  • 11,015
  • 2
  • 37
  • 41
2

Directly return the Component in the anonymous function inside map

.map((d, i) => (<Component />))

or

.map((d, i) => <Component />)
scazzy
  • 950
  • 2
  • 8
  • 20
0

See MDN docs:

The map() method creates a new array with the results of calling a provided function on every element in this array.

If the provided function doesn't return any data..you will have an array with undefined objects in it.

[1, 2, 3].map(function(){}) // [undefined, undefined, undefined]
atlanteh
  • 5,615
  • 2
  • 33
  • 54
0

As Bikas Vaibhav said in his comment and paraphrasing Mozilla Docs: map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results.

Therefore, if you do not return anything, you will have an array the same length as your ballArray only with undefined instead of a returned value. Try these two examples in your browser console.

With return:

> b = [1,4,9];
> d = b.map(function(a){
    var rt = a*2;
    return rt
});
<- [2, 8, 18]

Without return:

// Without a return:
> d = b.map(function(a){
    var rt = a*2;
});
<- [undefined, undefined, undefined]

Hope this helps!

edlee
  • 665
  • 1
  • 7
  • 20
0

With ES6 arrow functions you can do implicit returns like this:

this.state.ballArray.map((d, i) => (
  <Animated.View />
))
Matt Aft
  • 8,742
  • 3
  • 24
  • 37