小编Adr*_*ian的帖子

在 React.js 中向下滚动页面时如何加载更多搜索结果?

我想在向下滚动页面时加载更多搜索结果。我使用谷歌 API 书籍,我只想加载 10 个搜索结果,如果用户滚动到最后一个搜索元素,则自动加载接下来的 10 个结果。怎么做?我需要一个代码示例,正确的解决方案应该是什么样子。感谢帮助。

我的 App.js 文件如下:

 import React, { useState } from "react";
 import axios from "axios";
 import './App.css';
 import 'bootstrap/dist/css/bootstrap.min.css';


const App = () => {
const [searchTerm, setSearchTerm] = useState("");
const [books, setBooks] = useState({ items: [] });
const onInputChange = e => {
    setSearchTerm(e.target.value);
};

let API_URL = 'https://www.googleapis.com/books/v1/volumes';

const fetchBooks = async () => {

    if (document.getElementById("choose_search").value === "title") {

        const result = await axios.get(`${API_URL}? 
q=${searchTerm}&maxResults=40`);
        setBooks(result.data);
    }
    else {
        const result …
Run Code Online (Sandbox Code Playgroud)

html javascript frontend reactjs

12
推荐指数
2
解决办法
3万
查看次数

标签 统计

frontend ×1

html ×1

javascript ×1

reactjs ×1