0

react-infinite-scroll-component请告诉我如何在(npm 模块)的帮助下在我的反应应用程序中实现无限滚动 。我正在使用功能组件。代码如下:

import './App.css';
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import InfiniteScroll from 'react-infinite-scroll-component';

export default function App() {
  const [post, setPost] = useState([]);
  const url = 'http://localhost:3000/users';

  useEffect(() => {
    axios
      .get(url)
      .then((res) => {
        setPost(res.data);
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);

  const showUsers = post.map((item) => {
    return (
      <div className="card" key={item.id}>
        <h2>Name: {item.name}</h2>
        <h3>Username: {item.username}</h3>
        <p>Email:{item.email}</p>
      </div>
    );
  });

  return <div className="container">{showUsers}</div>;
}
4

0 回答 0