对本机基本选择器的项目进行条件渲染[React Native]

编程入门 行业动态 更新时间:2024-10-28 04:23:36
本文介绍了对本机基本选择器的项目进行条件渲染[React Native]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将"Native Base"组件用于我们的产品,并且在此方面做得很好, 但我只停留在一点,就是将项放入Nativebase Picker中.我的代码是这样的

I’m using ‘Native Base’ components for our product and going good with this, but I’m stuck at one point and it is around putting Items in Nativebase Picker. My code is like this

渲染方法代码-

render(){ return ( <View style={{marginTop: 20, flexDirection:'row', flexWrap:'wrap', justifyContent:'space-around', alignItems:'center'}}> <View style={{flex:1, justifyContent:'center', alignItems:'flex-end' }}> <Button style={{ backgroundColor: '#6FAF98', }} onPress={this._showDateTimePicker} > <Text>Select Date</Text> </Button> </View> <View style={{flex:1, justifyContent:'center', alignItems:'stretch'}}> <Picker style={{borderWidth: 1, borderColor: '#2ac', alignSelf:'stretch'}} supportedOrientations={['portrait','landscape']} iosHeader="Select one" mode="dropdown" selectedValue={this.state.leaveType} onValueChange={(value)=>this.setState({leaveType:value,}) //this.onValueChange.bind(this) }> <Item label="Full Day" value="leave1" /> { this.showStartDateFirstHalf() // Here I want to show this picker item on the basis of a condition } <Item label="2nd half" value="leave3" /> </Picker> </View> <DateTimePicker isVisible={this.state.isStartDatePickerPickerVisible} onConfirm={this._handleDatePicked} onCancel={this._hideDateTimePicker} mode='date' /> </View> ); } showStartDateFirstHalf() { if(!this.state.isMultipleDays) { return( <Item label="1st Half" value="leave2" /> ); } }

因此,如果this.state.isMultipleDays为false,则此代码可以正常工作,但是,当this.state.isMultipleDays为true时,则意味着当它位于else部分时,我会收到此错误-

So, this code is working fine if this.state.isMultipleDays is false, But when this.state.isMultipleDays is true, it means when it is in else part then i'm getting this error -

Cannot read property 'props' of undefined

推荐答案

我认为对此有一个更简单的答案.代替创建单独的showStartDateFirstHalf()函数,请尝试以下操作:

I think there's an easier answer to this. Instead of creating the separate showStartDateFirstHalf() function try this:

render() { const pickerItems = [ { label: 'Full Day', value: 'leave1', }, { label: '1st Half', value: 'leave2', }, { label: '2nd Half', value: 'leave3', }, ]; const filteredItems = pickerItems.filter(item => { if (item.value === 'leave2' && this.state.isMultipleDays) { return false; } return true; }); // The 'return' statement of your render function return ( ... <Picker ...> {(() => filteredItems.map(item => <Item label={item.label} value={item.value} /> )()} </Picker> ... ); }

这样,您已经具有在渲染周期的return语句之前确定的项目列表.同样,如果不满足条件,使用filter代替map不仅会给您null作为第二项,还会完全删除该项.

That way, you already have a list of items that is determined before the return statement of the render cycle. Also the use of filter instead of map will not just give you null as the second item if the condition is not met, but will remove the item altogether.

更多推荐

对本机基本选择器的项目进行条件渲染[React Native]

本文发布于:2023-11-25 17:08:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:本机   条件   项目   选择器   Native

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!