简体   繁体   English

React Native中条件渲染的映射

[英]Mapping of Conditional Rendering in React Native

I have a component in React Native that involves a map and markers that corresponds to certain locations. 我在React Native中有一个组件,其中涉及对应于某些位置的地图和标记。 Some markers have multiple items/images that are supposed to render, while others have one or none. 有些标记具有应该渲染的多个项目/图像,而另一些则没有或只有一个。 I can make it render on each of the markers but I can't get my items to render on a marker when there are multiple. 我可以使它在每个标记上呈现,但是当有多个标记时,我无法使我的项目在标记上呈现。

If I run a console log, location 105 for example with have 5 in the array, 4 at location 104, location 101 will have 1, and 106 will have 0. I want to make it so that it will return a 5 logos in 105, 4 at 104 1 in 101, etc. 如果运行控制台日志,例如位置105数组中有5个,位置104中有4个,位置101中将有1个,而位置106中将有0个。我要创建它以便在105中返回5个徽标,4在104 1在101中,依此类推。

Currently I can get it to return the text of Hello at each of locations, but this is not what I'm trying to do. 目前,我可以在每个位置返回它来返回Hello文本,但这不是我要尝试的操作。 I'm having trouble with the conditional mapping. 我在条件映射方面遇到麻烦。

 addSupporters(markerLocation, teamImages){ var teamLogos = [] for (i = 0; i < this.props.supportersGroups.MLS.length; i++) { if (this.props.supportersGroups.MLS[i].homebar == markerLocation.MapMarker.id){ teamLogos.push(this.props.supportersGroups.MLS[i]) return( <View> <Text>{this.props.supportersGroups.MLS[i].name}</Text> <Image style={{width: 25, height: 25}} source={teamImages[this.props.supportersGroups.MLS[i].id]} /> </View> ) } } } 

Currently it will render the first item that meets the condition, but I would like it to render all of the items that meet the condition. 当前它将渲染第一个满足条件的项目,但是我希望它渲染所有符合条件的项目。 So whenever the this.props.supportersGroups.MLS[i].homebar is equal to the markerLocation.MapMarker.id it adds them to the teamLogos array and then the array is mapped. 因此,只要this.props.supportersGroups.MLS [i] .homebar等于markerLocation.MapMarker.id,就会将它们添加到teamLogos数组中,然后对该数组进行映射。 So if there are 3 locations that meet the array, then it will look like this for this specific markerLocation: 因此,如果有3个符合阵列的位置,则此特定markerLocation的外观如下:

  <View> <Text>{this.props.supportersGroups.MLS[0].name}</Text> <Image style={{width: 25, height: 25}} source={teamImages[this.props.supportersGroups.MLS[0].id]} /> <Text>{this.props.supportersGroups.MLS[4].name}</Text> <Image style={{width: 25, height: 25}} source={teamImages[this.props.supportersGroups.MLS[4].id]} /> <Text>{this.props.supportersGroups.MLS[6].name}</Text> <Image style={{width: 25, height: 25}} source={teamImages[this.props.supportersGroups.MLS[6].id]} /> </View> 

When we call addSupporters() can return an object which satisfies has values of this.props.supportersGroups.MLS where Groups.MLS[i].homebar is equal to the markerLocation.MapMarker.id and pass this array to be displayed. 当我们调用addSupporters()可以返回满足以下条件的对象: this.props.supportersGroups.MLS ,其中Groups.MLS[i].homebar is equal to the markerLocation.MapMarker.id并传递此数组以显示。 For that change your addSupporters function as, 为此,您的addSupporters功能如下:

addSupporters(markerLocation, teamImages){
    var teamLogos = []
    this.props.supportersGroups.MLS.filter((mls)=>mls.homebar == markerLocation.MapMarker.id)
      }     
    }
    return teamLogos;
  }

and in render call the addSupporters() with correct parameters and, 然后在渲染器中使用正确的参数调用addSupporters()

render(){
let teamLogos = addSupporters(markerLocation, teamImages)
return(
...
{
   teamLogos.map((logo)=><View>
        <Text>{logo.name}</Text>
        <Image
          style={{width: 25, height: 25}}
          source={logo.id]}
        />
      </View>)
}

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

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