Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
How to set searchbar in react native

I am a new in react native i am try to set search bar in my application. To search in flatlist data. i am not able to search in flatlist data ,What is the error i don't know. 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>

  );
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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.

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 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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda