【问题标题】:render component based on selected item in array view基于数组视图中的选定项渲染组件
【发布时间】:2019-02-28 14:51:03
【问题描述】:

我正在处理一个项目,我需要从数组中提供一个项目列表,其中一个项目设置为默认值。如果用户单击另一个项目,则复选标记会从默认项目移动到选定项目。

例如,如果我有数组 [default: "Saab", "Volvo", "BMW"]。然后,如果我单击沃尔沃,它将采用默认属性。我已经过滤掉了数组并将其更新为新选择的项目。一旦它们已经呈现,我在视图组件上显示它时遇到问题。我的过滤功能是

selectItem(val){
  var currentDefault = this.state.vehicleList.find(function(element, index) {
    return element.default == true;
  });

  var currentVehicle = this.state.vehicleList.indexOf(currentDefault);
  var selectedItem = this.state.vehicleList.indexOf(val);

  if (currentVehicle != selectedItem){
    this.state.vehicleList[currentVehicle].default = false;
    this.state.vehicleList[selectedItem].default = true;
    this.state.pickedVehicle = this.state.vehicleList[selectedItem];
  } else {
    console.log('same vehicle');
  }   
}

我是否应该将项目置于状态以使其更改?

我在下面添加渲染功能。

_renderItem({item, key}) {

      const car = (<Icon name="car" size={16} color="grey" />)
      const bicycle = (<Icon name="bicycle" size={16} color="grey" />)
     color="black" />)
      const check = (<Icon name="check-square" size={30} color="green" />)



                        if (item.type == 'car'){
                            return (
                              <TouchableHighlight onPress={() => this.selectItem(item)} underlayColor={'transparent'}>
                                <LinearGradient key={key} style={{justifyContent: 'center', borderRadius: 30, width: 180, height: 120, alignSelf: 'center'}} colors={['#ff00ff', '#0066ff']}>
                                    {renderIf(item.default == true)(
                                            <Text key={key} style={{alignSelf: 'flex-end', backgroundColor: 'rgba(0,0,0,0)', marginRight: 5}}> 
                                              {check}
                                             </Text>
                                        )}
                                    <View style={{alignSelf: 'center', justifyContent: 'center', width: 40, height: 40, borderRadius: 60/2, backgroundColor: 'white'}}>
                                        <Text style={{alignSelf: 'center'}}>
                                        {car} 
                                        </Text>
                                    </View>
                                    <Text style={{marginTop: 5, fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 12, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}>
                                      {item.make} {item.model} {item.year}
                                    </Text>
                                    <Text style={{fontWeight: 'bold', color: 'white', fontSize: 12, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}>
                                     {item.licensePlate} 
                                    </Text>
                                </LinearGradient>
                              </TouchableHighlight>
                              )
                          }
                          if (item.type == 'bicycle') {
                            return (
                                <LinearGradient key={key} style={{justifyContent: 'center', borderRadius: 30, width: 180, height: 120, alignSelf: 'center'}} colors={['#99cc00', '#000099']}>
                                      {renderIf(item.default == true)(
                                            <Text key={key} style={{alignSelf: 'flex-end', backgroundColor: 'rgba(0,0,0,0)', marginRight: 5}}> 
                                              {check}
                                             </Text>
                                        )}
                                        <View style={{alignSelf: 'center', justifyContent: 'center', width: 40, height: 40, borderRadius: 60/2, backgroundColor: 'white'}}>
                                            <Text style={{alignSelf: 'center'}}>
                                            {bicycle} 
                                            </Text>
                                        </View>
                                        <Text style={{marginTop: 5, fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 12, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}>
                                          {item.make} {item.model} {item.year}
                                        </Text>
                                        <Text style={{fontWeight: 'bold', color: 'white', fontSize: 12, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}>
                                         {item.licensePlate} 
                                        </Text>
                                </LinearGradient>

                              )
                            }

我如何添加组件

{this.state.vehicleList[index].default  && (
                                            <Text key={key} style={{alignSelf: 'flex-end', backgroundColor: 'rgba(0,0,0,0)', marginRight: 5}}> 
                                              {check}
                                             </Text>
                                        )}

【问题讨论】:

  • 是的,您需要在渲染时使用这些车辆的默认状态。你能分享你渲染组件的代码吗?
  • 刚刚更新了@Chandini

标签: javascript arrays react-native render


【解决方案1】:

由于对何时在项目旁边显示复选图标有限制,您可以在渲染时将该条件与图标一起添加。比如你需要根据这个条件显示 Icon 组件,你应该像这样添加它。

{this.state.vehicleList[indexOfTheItem].default && (<Icon/>)}

仅当 this.state.vehicleList[item].default 为 true 时才会呈现图标

当状态改变时,组件会再次渲染。在更新每辆默认车辆的状态时,您可以使用相应车辆的布尔值作为其旁边的条件。

【讨论】:

  • 好的,但是如何使图标从一项更改为另一项。 item.default 在第一次渲染视图时运行一次。能否提供更多细节?
  • 将 this.state.vehicleList[indexOfTheItem] 条件和图标放置在每个项目旁边,任何时候只有其中一个是默认的并且处于选中状态。因此,对于状态为 true 的项目,图标将仅显示在该项目旁边。如果我不清楚,请告诉我。
  • 好的,所以我添加了下面的代码,但我的复选标记没有移动。 @钱迪尼。再次感谢你。这个真的让我很受鼓舞,我觉得我错过了一些小东西。
  • 您确定您为项目的索引传递了正确的值吗?而且,每当单击任何项​​目时,您都会更新所有项目 ryt 的默认状态?
  • 好的。你能像这样将条件放在 Text 组件内和 Icon 旁边吗? {this.state.vehicleList[index].default && check }
猜你喜欢
  • 1970-01-01
  • 2021-06-12
  • 2018-11-02
  • 2022-11-01
  • 2018-05-29
  • 2020-06-04
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多