首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在React-native中创建下拉菜单?

如何在React-native中创建下拉菜单?
EN

Stack Overflow用户
提问于 2021-02-04 19:40:42
回答 4查看 834关注 0票数 2

我想在我的视图中添加一个下拉列表。我是新手,请帮助我。

我还添加了一个评论,我试图添加一个下拉列表。我尝试了这个https://www.npmjs.com/package/react-native-dropdown-picker,但它只是给了我一些错误。

从‘react’导入React,{ useState };//从‘react-native-dropdown-picker’导入useState DropDownPicker;从‘react-native’导入{ StyleSheet,Text,View,TextInput,StatusBar,SafeAreaView,图像,按钮,警报};

代码语言:javascript
复制
    export default function App() {
    
        const [search, setSearch] = useState(''); //add this line
        
      return (      
        
        <SafeAreaView>
          <View style={styles.container}>
            <TextInput 
              onChangeText={(text) => setSearch(text)}
              
              placeholder="Search"
              style={styles.searchbox}                        
            ></TextInput>   
    
          <View style={styles.makecentral} >   
            {search.length < 1 ? <Image               
                style={styles.tinyLogo}
                source={require('./assets/icons8_search_200px_3.png')}        
              /> : ( 
                  null  //add clear text image and clear search text
                  
              )}
          
          </View>
                <View style={{flexDirection:'row'}}>
    
                  <View elevation={2}  style={{flex:3,backgroundColor:'#ffffff',height:40,marginTop:10,marginLeft:'2.5%',borderBottomLeftRadius:5,borderTopLeftRadius:5}}>
                    <Text style={{textAlign:'center',fontSize:20,color:'#FF7B00',marginTop:7}}>
                      Category
                    </Text>
                  </View>
                  
                  <View elevation={2} style={{backgroundColor:'#FF7B00',height:40,flex:5,marginTop:10,borderTopRightRadius:5,borderBottomRightRadius:5,marginRight:'2.5%'}}>

                   //here..I want to add a dropdown..
       
                  </View>
    
                
                </View>
    
        </View>
        </SafeAreaView>
      );
    }
    
    
    const styles = StyleSheet.create({
      container: {    
        backgroundColor: '#fff',    
        
      },
    
      searchbox:{
        backgroundColor:'#f2f2f2',
        marginTop : StatusBar.currentHeight+5,
        height : 50,
        marginLeft:10,
        marginRight : 10,
        borderRadius : 20,
        textAlignVertical:'center',
        textAlign : 'center',
        alignItems:'center',        
      },
    
      tinyLogo: {
        position : 'absolute',
        width: 30,
        height: 30,
        opacity: 0.5,
        marginTop: -40,    
      },
    
      makecentral: {
        alignItems:'center',
        marginRight:80,    
      },
    
      category:{
        backgroundColor:'#f2f2f2',
        height:50,
        width:'90%',
        alignContent:'center',
    
    
      }
    });
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-02-26 21:45:58

您可以使用其他组件库,如Ui Kitten。其中包含一个更好的下拉列表。

这是下拉列表的特定链接:- Link

您还可以使用许多其他库。考虑一下:- top libraries

票数 1
EN

Stack Overflow用户

发布于 2021-02-04 20:13:32

从你的回答看来,你没有给你的DropDownPicker提供items,因此它崩溃了。你不能仅仅给出一个空的组件。

根据文档,它应该类似于:

代码语言:javascript
复制
<DropDownPicker
    items={[
        {label: 'USA', value: 'usa', icon: () => <Icon name="flag" size={18} color="#900" />, hidden: true},
        {label: 'UK', value: 'uk', icon: () => <Icon name="flag" size={18} color="#900" />},
        {label: 'France', value: 'france', icon: () => <Icon name="flag" size={18} color="#900" />},
    ]}
    defaultValue={this.state.country}
    containerStyle={{height: 40}}
    style={{backgroundColor: '#fafafa'}}
    onChangeItem={item => this.setState({
        country: item.value
    })}
/>

items实际上是唯一需要的道具。

请参考here

票数 1
EN

Stack Overflow用户

发布于 2021-04-04 10:22:35

以下是可用于在react原生中创建下拉选择器的库:

react-native-dropdown-picker

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66044866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档