简体   繁体   English

无法在 react native 中显示 flatlist 中的所有数据

[英]Not able to show all the data in flatlist in react native

I have a list of hospitals name which is coming from api.我有一个来自 api 的医院名称列表。 I have made a search functionality, where text typed by user will be matched with hospital name and resulted data will be shown on screen.我做了一个搜索功能,用户输入的文本将与医院名称匹配,结果数据将显示在屏幕上。

Below is my code:下面是我的代码:

const FindNameScreen = (props: any) => {
    const [search, setSearch] = useState('');
    const [filteredDataSource, setFilteredDataSource] = useState([]);
    const [masterDataSource, setMasterDataSource] = useState([]);

    const gethospitalList = useCallback(() => {
        Apicall
            .then(async (resp) => {
                // console.log('resp>>>>>>>>>>>>>>>>>>>>>', resp.data);
                if (resp) { setMasterDataSource(resp.data); } 
                else { Alert.alert('Error', resp); }
            })
            .catch((err: any) => {
                console.log(err);
            });
    }, []);

    useEffect(() => { gethospitalList();}, [gethospitalList]);


    const searchFilterFunction = (text: any) => {
        if (text) {
            const newData = masterDataSource.filter(function (item: any) {
                const itemData = item.hospital_name.toLowerCase();
                const textData = text.toLowerCase();
                return itemData.indexOf(textData) > -1;
            });
            setFilteredDataSource(newData);
            setSearch(text);
        } else {
            setFilteredDataSource(masterDataSource);
            setSearch(text);
        }
    };

    const ItemView = ({item}) => {
        return (
                <View>
                    <Text style={styles.itemStyle}>{item.hospital_name}</Text>
                </View>
        );
    };

    return (
        <>
            <TextInput
            style={styles.textInputStyle}
            onChangeText={(text) => searchFilterFunction(text)}
            value={search}
            underlineColorAndroid="transparent"
            placeholder="Search Here"
            selectionColor="#4FE6AF"
            />
            <View>
                <FlatList
                data={filteredDataSource}
                keyExtractor={(item, index) => index.toString()}
                renderItem={ItemView}
                />
            </View>
        </>
    );
};
export default FindNameScreen;

Initially I have to show all the hospital names, and then when user types something then according to that the list are getting updated.最初我必须显示所有医院名称,然后当用户输入内容时,根据该列表正在更新。 All of these are working fine.所有这些都运行良好。

My problem is when the TextInput is empty, all the hospital name should be shown, but sometimes I am getting only two name, sometimes I am getting all the name.我的问题是当TextInput为空时,应该显示所有医院名称,但有时我只得到两个名称,有时我得到所有名称。 This should not happen.这不应该发生。 When TextInput is empty then all the names should be shown in list.TextInput为空时,所有名称都应显示在列表中。

Initially, your Flatlist won't render any elements because the filteredDataSource state is blank at the beginning.最初,您的 Flatlist 不会呈现任何元素,因为filteredDataSource状态在开始时是空白的。

searchFilterFunction will only be triggered once you type something in TextInput. searchFilterFunction只有在您在 TextInput 中输入内容时才会触发。 So, to display all the names at the beginning, try this:因此,要在开头显示所有名称,请尝试以下操作:

<FlatList
    data={
      search === '' && filteredDataSource.length === 0
        ? masterDataSource
        : filteredDataSource
    }
    keyExtractor={(item, index) => index.toString()}
    renderItem={ItemView}
  />

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

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