简体   繁体   English

如何在 React Native 中设置搜索栏

[英]How to set searchbar in react native

I am a new in react native i am try to set search bar in my application.我是 React Native 的新手,我正在尝试在我的应用程序中设置搜索栏。 To search in flatlist data.在平面列表数据中搜索。 i am not able to search in flatlist data,What is the error i don't know.我无法在 flatlist 数据中搜索,我不知道错误是什么。 how can i set how to resolve my problem please resolve my issue.我如何设置如何解决我的问题请解决我的问题。 i have attached the my code given below.我附上了下面给出的我的代码。

import React from 'react';
import { StyleSheet, FlatList, View, TouchableOpacity, Image, Text, TextInput } from 'react-native';
import { dishesData } from './DishesData';
import { SearchBar } from 'react-native-elements'

const MenuListScreen = ({ navigation }) => {
  const state = {
    searchText: "",
    dishesData:[],
    filteredData: [],
  };
  let search = (searchText) => {
    this.setState({searchText: searchText});
  
    let filteredData = state.dishesData.filter(function (item) {
      return item.description.includes(searchText);
    });
  
    this.setState({filteredData: filteredData});
  };
  return (

    <View style={styles.container}>
      <SearchBar
        round={true}
        lightTheme={true}
        placeholder="Search..."
        autoCapitalize='none'
        autoCorrect={false}
        onChangeText={search}
        value={state.searchText}
      />

      <FlatList style={styles.list}
        contentContainerStyle={styles.listContainer}
        data={dishesData}
        horizontal={false}
        numColumns={1}
        keyExtractor={(item) => {
          return item.id;
        }}
        renderItem={({ item }) => {
          return (
            <TouchableOpacity
              onPress={() => navigation.navigate('MenuDetail', { id: item.id })}
            >
              <Image
                style={styles.userImage}
                source={{ uri: item.imageSource }}
              />
              <View style={styles.cardFooter}>
                <View style={{ alignItems: "center", justifyContent: "center" }}>
                  <Text style={styles.name}>{item.title}</Text>
                  <Text style={styles.position}>{item.price}</Text>
                  {/* <TouchableOpacity style={styles.followButton}
                    onPress={() =>
                      props.navigation.navigate('DetailsPage', item)
                    }>
                    <Text style={styles.followButtonText}>Buy Now</Text>
                  </TouchableOpacity> */}
                </View>
              </View>
            </TouchableOpacity>
          )
        }} />

    </View>

  );
};

Hi there you can filter items by using filter function and you are already doing that but for accuracy I'll suggest to do like this.您好,您可以使用过滤器 function 来过滤项目,您已经在这样做了,但为了准确起见,我建议您这样做。

let search = (searchText) => {
    this.setState({searchText: searchText});
  
    let filteredData = state.dishesData.filter(function (item) {
      return item.description.toUpperCase().includes(searchText.toUpperCase());
    });
  
    this.setState({filteredData: filteredData});
  };

and to render the filtered items on Flatlist you have to toggle data array on FlatList并在 FlatList 上呈现过滤后的项目,您必须在Flatlist上切换数据数组

<FlatList style={styles.list}
        contentContainerStyle={styles.listContainer}
        data={state.searchText ? state.filteredData:state.dishesData}
        horizontal={false}
        numColumns={1}
        keyExtractor={(item) => {
          return item.id;
        }}.../>

Here is the Snack Link you can test as well Working Example这是您也可以测试的小吃链接工作示例

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

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