【发布时间】: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