React Native 错误类型错误:keyword.toLowerCase不是函数,(在'keyword.toLowerCase()'中,'keyword.toLowerCase'未定义)

bmp9r5qi  于 2022-12-14  发布在  React
关注(0)|答案(1)|浏览(136)

我如何修复这个错误,说错误TypeError:keyword.toLowerCase不是一个函数。(在'keyword.toLowerCase()'中,'keyword.toLowerCase'未定义)我正在rn中使用js filter构建一个搜索用户函数,所以我将搜索到的关键字转换为小写,但现在我面临这个错误,我该如何修复它?我做错了什么?
我试着在小写方法之前使用ToString方法,但它在我的情况下不起作用,有人能告诉我,我哪里错了吗?

import { StyleSheet, Text, View } from 'react-native'
import React, { useState } from 'react'
import { ScrollView, TextInput } from 'react-native-gesture-handler'
import { Ionicons } from '@expo/vector-icons';
import { formHead } from '../../CommonCss/FormCss';
import ChatCard from '../../Cards/ChatCard';


  const MainChat = ({ navigation }) => {
    
        const chats = [
   //  here is raw data of users i remove it bcs than code will hard to read the data was too long
            ]
    
        const [keyword, setKeyword] = useState('')
    
        return (
            <ScrollView style={styles.container}>
                <Ionicons name="arrow-back" size={24} color="grey" style={styles.backbtn}
                    onPress={() => navigation.navigate("mainpage")}
                />
                <View style={styles.searchSection}>
                    <Text style={formHead}>Your Chats</Text>
                    <TextInput placeholder='Search'
                        style={styles.searchbar}
                        onChange={(text) => setKeyword(text)}
                    />
                </View>
                <View style={styles.ChatSection}>
                    {
                        chats.filter(
                            (chat) => {
                                if (keyword == '') {
                                    return chat
                                }
                                else if (
                                    chat.username.
                                        toLowerCase().includes
                                        (keyword.toLowerCase())
                                ) {
                                    return chat
                                }
    
                            }
                        ).map((chat) => {
                            return <ChatCard key={chat.username} chat={chat} />
                        })
                    }
                </View>
            </ScrollView>
        )
    }
    
    export default MainChat
knpiaxh1

knpiaxh11#

TextInput应如下所示。更改onChangeText而不是onChange

<TextInput placeholder='Search'
   style={styles.searchbar}
   onChangeText={(text) => setKeyword(text)}
/>

相关问题