javascript - React-native 搜索栏 - 错误 : undefined is not a function (near '... this.state.books.filter...' )

标签 javascript firebase react-native firebase-realtime-database searchbar

我不知道我在这里做错了什么。

我添加了一个搜索栏,但是当我在文本字段中写入内容时,它给出了错误:“未定义不是一个函数(靠近'... this.state.books.filter...')”

下面是我的一些代码。

我做错了什么?

 state = {
    books: {},
};
componentDidMount() {
        firebase
        .database()
        .ref('/Books')
        .on('value', snapshot => {
            this.setState({ books: snapshot.val() });
        });
}
searchBooks = value => {
    const { books } = this.state;
    const bookArray = Object.values(books);
    const filteredBooks = bookArray.filter(book => {
        let bookLowercase = (book.bookname).toLowerCase();
        let searchTermLowercase = value.toLowerCase();
        return bookLowercase.indexOf(searchTermLowercase) > -1;
    });
    this.setState({ books: filteredBooks });
};

render() {
    const { books } = this.state;
    if (!books) {
        return null;
    }
    const bookArray = Object.values(books);
    const bookKeys = Object.keys(books);
    return (

        <View style={styles.container}>
            {/*Searchbar */}
            <View style={{ height: 80, backgroundColor: '#c45653', justifyContent: "center", paddingHorizontal: 5 }}>
                <View style={{ height: 50, backgroundColor: 'white', flexDirection: 'row', padding: 5, alignItems: 'center' }}>
                    <TextInput
                        style={{
                            fontSize: 24,
                            marginLeft: 15
                        }}
                        placeholder="Search"
                        onChangeText={value => this.searchBooks(value)}
                    />
                </View>
            </View>

            <FlatList
                style={{ backgroundColor: this.state.searchBarFocused ? 'rgba(0,0,0,0.3)' : 'white' }}
                data={bookArray}
                keyExtractor={(item, index) => bookKeys[index]}
                renderItem={({ item, index }) => (
                    <BookListItem
                        book={item}
                        id={bookKeys[index]}
                        onSelect={this.handleSelectBook}
                    />
                )}
            />
        </View>
    );

最佳答案

您正在将 this.state.books 初始化为 Object .

state = {
    books: {},
};

对象没有过滤功能。您可能想使用 Array相反。

state = {
    books: [],
};

然后您就可以将 this.state.books.filter() 与数组的 filter 一起使用功能。

关于javascript - React-native 搜索栏 - 错误 : undefined is not a function (near '... this.state.books.filter...' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58858343/

相关文章:

javascript - Firebase - 将额外的用户数据添加到单独的数据库

android - 如何在我的 android 应用程序中使用 firebase 集成 otp 验证

javascript - 动态设置 Apollo 客户端 header 不起作用

php - Jqgrid自定义格式如果为负值则使用括号()

javascript - $(this) 在我的 jQuery 点击函数中不起作用

android - 如何将 firebase 身份验证与 Android 应用程序的 instagram 登录集成

reactjs - 如何用 Jest 模拟 react 导航参数值

react-native - 即使部署了新代码,Expo 仍使用旧代码

java - 如何将webview内容划分为多个页面

javascript - 有没有办法阻止 shadow dom 中的 javascript 访问它之外的 DOM?