【问题标题】:Load more implementation in ReactJs在 ReactJs 中加载更多实现
【发布时间】:2020-04-20 15:37:40
【问题描述】:

我正在尝试为我的小型项目 GiF 生成器实现加载更多按钮。首先,我想在底部附加下一组 20 个响应,但没有做到。 接下来,我想通过简单地删除当前结果来实现加载下一组 20 个结果。我试图在单击按钮时触发一个方法,但我没有这样做。它在第二次单击加载更多时更新状态,然后不再更新它。 请帮我找出我缺少的东西,我昨天已经开始学习 React。

import React, { useEffect, useState } from 'react';
import './App.css';
import Gif from './Gif/Gif';

const App = () => {
  const API_KEY = 'LIVDSRZULELA';

  const [gifs, setGif] = useState([]);
  const [search, setSearch] = useState('');
  const [query, setQuery] = useState('random');
  const [limit, setLimit] = useState(20);
  const [pos, setPos] = useState(1);

  useEffect(() => {
    getGif();
  }, [query])

  const getGif = async () => {
    const response = await fetch(`https://api.tenor.com/v1/search?q=${query}&key=${API_KEY}&limit=${limit}&pos=${pos}`);
    const data = await response.json();
    setGif(data.results);
    console.log(data.results)
  }

  const updateSearch = e => {
    setSearch(e.target.value);
  }

  const getSearch = e => {
    e.preventDefault();
    setQuery(search);
    setSearch('');
  }

  const reload = () => {
    setQuery('random')
  }

  const loadMore = () => { // this is where I want my Pos to update with 21 on first click 41 on second and so on
    let temp = limit + 1 + pos;
    setPos(temp);
    setQuery(query);
  }

  return (
    <div className="App">
      <header className="header">
        <h1 className="title" onClick={reload}>React GiF Finder</h1>
        <form onSubmit={getSearch} className="search-from">
          <input className="search-bar" type="text" value={search}
            onChange={updateSearch} placeholder="type here..." />
          <button className="search-button" type="submit">Search</button>
        </form>
        <p>showing results for <span>{query}</span></p>
      </header>
      <div className="gif">
        {gifs.map(gif => (
          <Gif
            img={gif.media[0].tinygif.url}
            key={gif.id}
          />
        ))}
      </div>
      <button className="load-button" onClick={loadMore}>Load more</button>
    </div>
  );
}

export default App;

请帮我找出我做错了什么,据我所知,我将更新 setQuery 的那一刻 useEffect 应该用新的输入调用,但它没有发生。

【问题讨论】:

  • 我想你只需要将pos添加到useEffect依赖数组即可。
  • 是的,我也在尝试相同的方法,但它只是丢失了。但为什么?你能详细说明一下吗? @RashadKokash
  • 因为当您单击加载更多按钮时,您只会更新pos 而不是query,但您的useEffect 仅在query 更新时才会被调用。因此,如果您希望在更改 pos 时调用 useEffect,则需要将其添加到依赖项中。
  • 哦,知道了,我正在更新具有相同内容的查询,所以从逻辑上讲,我从未更新过。太感谢了! @RashadKokash

标签: javascript reactjs api load


【解决方案1】:

不妨试试这样的:


  // Fetch gifs initially and then any time
  // the search changes.
  useEffect(() => {
    getGif().then(all => setGifs(all);
  }, [query])

  // If called without a position index, always load the
  // initial list of items.
  const getGif = async (position = 1) => {
    const response = await fetch(`https://api.tenor.com/v1/search?q=${query}&key=${API_KEY}&limit=${limit}&pos=${position}`);
    const data = await response.json();
    return data.results;
  }


  // Append new gifs to existing list
  const loadMore = () => {
    let position = limit + 1 + pos;
    setPos(position);
    getGif(position).then(more => setGifs([...gifs, ...more]);
  }

  const getSearch = e => {
    e.preventDefault();
    setQuery(search);
    setSearch('');
  }

  const updateSearch = e => setSearch(e.target.value);

  const reload = () => setQuery('random');

基本上,让getGifs 方法更通用一点,然后如果调用loadMore,则从getGift 获取下一个gif 列表并附加到现有的gif 列表中。

【讨论】:

  • 完美!太感谢了!现在我能够实现我想要的。再次感谢。
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2017-05-09
相关资源
最近更新 更多