Load more 只在从 API 中获取数据时起作用一次,React 中。

huangapple go评论108阅读模式
英文:

Load more only works once when fetching from API in react

问题

I am currently trying to implement the load more functionality into this website, and I have a function I call when pressing the button, it adds 5 to the count, then this count is fetched, but the button only seems to add 5 the first time

我目前正在尝试将“加载更多”功能添加到这个网站中,当按下按钮时,我调用一个函数,它将数字增加5,然后获取该数字,但按钮似乎只在第一次添加5。

I don't really know what should be done, maybe using the useEffect, but I think is something really simple I can't see

我不太清楚应该做什么,也许可以使用 useEffect,但我认为这是一些非常简单的东西,我无法看到

  1. import React, { useEffect, useState } from "react";
  2. import "./BeerList.css";
  3. const BeerList = () => {
  4. const [beerList, setBeerList] = useState(null);
  5. let count = 10;
  6. const loadBeers = async () => {
  7. const response = await fetch(
  8. `https://api.punkapi.com/v2/beers?per_page=${count}`
  9. );
  10. const results = await response.json();
  11. const randomBeerList = results.map((beersObj) => ({
  12. name: beersObj.name,
  13. tagline: beersObj.tagline,
  14. url: beersObj.image_url,
  15. }));
  16. setBeerList(randomBeerList);
  17. };
  18. useEffect(() => {
  19. loadBeers();
  20. }, []);
  21. const increment = () => {
  22. count += 5;
  23. loadBeers();
  24. console.log(count);
  25. }
  26. if (beerList === null) {
  27. return <div>Loading...</div>;
  28. }
  29. return (
  30. <div className="beer-list-parent">
  31. <div className="beer-list">
  32. {beerList.map(({ name, tagline, url }, key) => (
  33. <div key={key} className="beer">
  34. <div className="beer-img">
  35. <img src={url} className="url" />
  36. </div>
  37. <div className="beer-text">
  38. <div className="name">{name}</div>
  39. <div className="tagline">"{tagline}"</div>
  40. </div>
  41. </div>
  42. ))}
  43. </div>
  44. <div id="loadmore" className="more-parent">
  45. <button className="more" type="button" onClick={increment}>Load more</button>
  46. </div>
  47. </div>
  48. );
  49. };
  50. export default BeerList;

I tried using a counter and incrementing but only is added once, I console.log and the value was repeating every time.

我尝试使用一个计数器并递增,但只添加了一次,我使用 console.log 发现值每次都重复。

英文:

I am currently trying to implement the load more functionality into this website, and I have a function I call when pressing the button, it adds 5 to the count, then this count is fetched, but the button only seems to add 5 the first time

I don't really know what should be done, maybe using the useeffect, but I think is something really simple I can't see

  1. import React, { useEffect, useState } from &quot;react&quot;;
  2. import &quot;./BeerList.css&quot;;
  3. const BeerList = () =&gt; {
  4. const [beerList, setBeerList] = useState(null);
  5. let count = 10;
  6. const loadBeers = async () =&gt; {
  7. const response = await fetch(
  8. `https://api.punkapi.com/v2/beers?per_page=${count}`
  9. );
  10. const results = await response.json();
  11. const randomBeerList = results.map((beersObj) =&gt; ({
  12. name: beersObj.name,
  13. tagline: beersObj.tagline,
  14. url: beersObj.image_url,
  15. }));
  16. setBeerList(randomBeerList);
  17. };
  18. useEffect(() =&gt; {
  19. loadBeers();
  20. }, []);
  21. const increment = () =&gt; {
  22. count += 5;
  23. loadBeers();
  24. console.log(count);
  25. }
  26. if (beerList === null) {
  27. return &lt;div&gt;Loading...&lt;/div&gt;;
  28. }
  29. return (
  30. &lt;div className=&quot;beer-list-parent&quot;&gt;
  31. &lt;div className=&quot;beer-list&quot;&gt;
  32. {beerList.map(({ name, tagline, url }, key) =&gt; (
  33. &lt;div key={key} className=&quot;beer&quot;&gt;
  34. &lt;div className=&quot;beer-img&quot;&gt;
  35. &lt;img src={url} className=&quot;url&quot; /&gt;
  36. &lt;/div&gt;
  37. &lt;div className=&quot;beer-text&quot;&gt;
  38. &lt;div className=&quot;name&quot;&gt;{name}&lt;/div&gt;
  39. &lt;div className=&quot;tagline&quot;&gt;&quot;{tagline}&quot;&lt;/div&gt;
  40. &lt;/div&gt;
  41. &lt;/div&gt;
  42. ))}
  43. &lt;/div&gt;
  44. &lt;div id=&quot;loadmore&quot; className=&quot;more-parent&quot;&gt;
  45. &lt;button className=&quot;more&quot; type=&quot;button&quot; onClick={increment}&gt;Load more&lt;/button&gt;
  46. &lt;/div&gt;
  47. &lt;/div&gt;
  48. );
  49. };
  50. export default BeerList;

I tried using a counter and incrementing but only is added once, I console.log and the value was repeating every time.

答案1

得分: 0

你每次组件渲染时(即每次状态更新时),都将count重置为0。为了避免这种情况,你应该将其存储在状态中。

此外,你还应该:(i) 在useEffect中获取数据,而不是在事件处理程序中获取数据,以避免竞争条件;(ii) 将count添加为useEffect的依赖项,因为它是一个依赖项,依赖项应始终位于依赖项数组中,这将允许useEffectcount更新时重新运行。

  1. import React, { useEffect, useState } from "react";
  2. import "./BeerList.css";
  3. const BeerList = () => {
  4. const [beerList, setBeerList] = useState(null);
  5. const [count, setCount] = useState(10); // <= 在这里进行更改
  6. const loadBeers = async () => {
  7. const response = await fetch(
  8. `https://api.punkapi.com/v2/beers?per_page=${count}`
  9. );
  10. const results = await response.json();
  11. const randomBeerList = results.map((beersObj) => ({
  12. name: beersObj.name,
  13. tagline: beersObj.tagline,
  14. url: beersObj.image_url,
  15. }));
  16. setBeerList(randomBeerList);
  17. };
  18. useEffect(() => {
  19. loadBeers();
  20. }, [count]); // <= 在这里进行添加
  21. const increment = () => {
  22. setCount(count + 5); // <= 在这里进行更改和移除
  23. console.log(count);
  24. }
  25. if (beerList === null) {
  26. return <div>Loading...</div>;
  27. }
  28. return (
  29. <div className="beer-list-parent">
  30. <div className="beer-list">
  31. {beerList.map(({ name, tagline, url }, key) => (
  32. <div key={key} className="beer">
  33. <div className="beer-img">
  34. <img src={url} className="url" />
  35. </div>
  36. <div className="beer-text">
  37. <div className="name">{name}</div>
  38. <div className="tagline">"{tagline}"</div>
  39. </div>
  40. </div>
  41. ))}
  42. </div>
  43. <div id="loadmore" className="more-parent">
  44. <button className="more" type="button" onClick={increment}>Load more</button>
  45. </div>
  46. </div>
  47. );
  48. };
  49. export default BeerList;
英文:

You're resetting count to 0 every time the component renders (which is every time the state updates). To avoid this you should store it in state.

Besides this, you should also: (i) fetch the data inside a useEffect and not in event handler to avoid race conditions; and (ii) add count as a dependency of your useEffect, because it is a dependency and dependencies should always be in the dependency array, and because this will allow for the useEffect to rerun every time count is updated.

  1. import React, { useEffect, useState } from &quot;react&quot;;
  2. import &quot;./BeerList.css&quot;;
  3. const BeerList = () =&gt; {
  4. const [beerList, setBeerList] = useState(null);
  5. const [count, setCount] = useState(10); // &lt;= Change here
  6. const loadBeers = async () =&gt; {
  7. const response = await fetch(
  8. `https://api.punkapi.com/v2/beers?per_page=${count}`
  9. );
  10. const results = await response.json();
  11. const randomBeerList = results.map((beersObj) =&gt; ({
  12. name: beersObj.name,
  13. tagline: beersObj.tagline,
  14. url: beersObj.image_url,
  15. }));
  16. setBeerList(randomBeerList);
  17. };
  18. useEffect(() =&gt; {
  19. loadBeers();
  20. }, [count]); // &lt;= Add here
  21. const increment = () =&gt; {
  22. setCount(count + 5); // &lt;= Change and remove here
  23. console.log(count);
  24. }
  25. if (beerList === null) {
  26. return &lt;div&gt;Loading...&lt;/div&gt;;
  27. }
  28. return (
  29. &lt;div className=&quot;beer-list-parent&quot;&gt;
  30. &lt;div className=&quot;beer-list&quot;&gt;
  31. {beerList.map(({ name, tagline, url }, key) =&gt; (
  32. &lt;div key={key} className=&quot;beer&quot;&gt;
  33. &lt;div className=&quot;beer-img&quot;&gt;
  34. &lt;img src={url} className=&quot;url&quot; /&gt;
  35. &lt;/div&gt;
  36. &lt;div className=&quot;beer-text&quot;&gt;
  37. &lt;div className=&quot;name&quot;&gt;{name}&lt;/div&gt;
  38. &lt;div className=&quot;tagline&quot;&gt;&quot;{tagline}&quot;&lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;/div&gt;
  41. ))}
  42. &lt;/div&gt;
  43. &lt;div id=&quot;loadmore&quot; className=&quot;more-parent&quot;&gt;
  44. &lt;button className=&quot;more&quot; type=&quot;button&quot; onClick={increment}&gt;Load more&lt;/button&gt;
  45. &lt;/div&gt;
  46. &lt;/div&gt;
  47. );
  48. };
  49. export default BeerList;

huangapple
  • 本文由 发表于 2023年2月24日 03:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定