简体   繁体   English

react-native:组件未在.map中呈现

[英]react-native: Component doesn't render in .map

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. 它在不使用return情况下不会呈现Animated.View ,尽管在许多示例中我已经看到了这项工作。 Any guesses why this behaviour? 任何猜测为什么会这样?

在此处输入图片说明

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. 我知道已经有几个答案,但是我相信他们无法解决OP 为何不起作用的特定问题,特别是考虑到新的ES6语法。

tl;dr: ES6 arrow functions with block bodies do not implicity return. tl; dr:带块体的ES6箭头函数不会隐式返回。

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. 在块体中,必须使用显式的return语句。

This is an example of "concise body", using the filter prototype: 这是使用filter原型的“简洁主体”示例:

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. 在您的文章中,您要在块体​​内指定组件而不返回它,并且由于地图原型要求您在用于新地图的回调中返回该项目,因此地图为空。

Directly return the Component in the anonymous function inside map 在地图内部的匿名函数中直接返回Component

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

or 要么

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

See MDN docs : 参见MDN 文档

The map() method creates a new array with the results of calling a provided function on every element in this array. map()方法创建一个新数组,并对该数组中的每个元素调用提供的函数。

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]

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. 正如Bikas Vaibhav在评论和对Mozilla Docs的解释中所说的:map依次为数组中的每个元素调用提供的回调函数,并从结果中构造一个新的数组。

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. 因此,如果您不返回任何内容,则您将拥有一个与ballArray相同长度的数组,只是未定义而不是返回值。 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! 希望这可以帮助!

With ES6 arrow functions you can do implicit returns like this: 使用ES6箭头函数,您可以像这样执行隐式返回:

this.state.ballArray.map((d, i) => (
  <Animated.View />
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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