前端react 开发 图书列表分页

我最近刚开始使用react 进行 实际的企业项目开发

网文系统

我平常使用vue多

最近突然猛地一用 react 说实话 多少有点不熟练 所以我现在开始一步一步总结

有一个列表的功能

显示一些图书

大概是这样 目前有一个列表 对接一个接口 然后 接上分页 功能

至于说样式 就慢慢写了

我直接上整个代码了

复制代码
import { Button, Card, Space, Image, Pagination, Modal } from 'antd'
import React, { useEffect, useState } from 'react'
import {
  http
  // , NumberInputChange 
} from '@/utils'

import "./index.scss"
import Operation from 'antd/es/transfer/operation'

const BookList = () => {
  const [list, setBookList] = useState([

  ])
  const [pageinfo, setPageInfo] = useState({
    currentPage: 1,
    pageSize: 10,
    total: 0
  })

  useEffect(() => {
    getlist()
  }, [pageinfo.currentPage, pageinfo.pageSize]) // 添加依赖,当
  const fetchUserList = async () => {
    return http.post('/api/Works/GetNovelList', {
      pageIndex: pageinfo.currentPage, // 使用当前页码
      pageSize: pageinfo.pageSize,     // 使用当前页大小
    }).then(res => {
      console.log(res)
      return res

      // return res.body.list.map(item => ({
      //   label: item.groupAlarmName,
      //   value: item.id,
      // }))
    })
  }

  const getlist = async () => {
    const res = await fetchUserList()
    console.log(res, "获取当前的结果")
    if (res.code == 200) {
      setBookList(res.data.infos)
      setPageInfo(prev => ({
        ...prev,
        total: res.data.totalCount
      }))
    } else {

    }



  }
  const handleBookAction = (e, name) => {
    console.log(e, name)

  }
  // 页码改变
  const handlePageChange = (page, pageSize) => {
    console.log('页码改变:', page, '页大小:', pageSize)
    setPageInfo(prev => ({
      ...prev,
      currentPage: page,
      pageSize: pageSize
    }))
    // 不要在这里手动调用 getlist(),useEffect 会自动触发
  }

  // 页大小改变
  const handleSizeChange = (current, size) => {
    console.log('页大小改变:', '当前页:', current, '新大小:', size)
    setPageInfo(prev => ({
      ...prev,
      currentPage: 1, // 重要:改变页大小时回到第一页
      pageSize: size
    }))
  }
  const [isModalOpen, setIsModalOpen] = useState(false)
  const handleOk = () => {
    alert("我点击了确定按钮")
    setIsModalOpen(false)

  }
  const handleCancel = () => {
    setIsModalOpen(false)
  }
  const openModal = () => {
    setIsModalOpen(true)

  }
  return (
    <div style={{ padding: '20px' }}>
      <Card size="small" title="我的作品" extra={<Button onClick={openModal} type='primary'>添加新书</Button>}>
        <div className='book-container'>
          {
            list.map((item) => {
              return <Card key={item.novelID} size='small' style={{ marginBottom: '16px' }} actions={[
                <Button type="link" onClick={() => handleBookAction(item, 'edit')}>编辑</Button>,
                <Button type="link" onClick={() => handleBookAction(item, 'chapters')}>章节管理</Button>,
                <Button type="link" onClick={() => handleBookAction(item, 'publish')}>发布</Button>
              ]}>
                {/* {item.novelName} */}
                <div className='item'>
                  <Image src={item.coverUrl} width={150} style={{ objectFit: 'widthFix', borderRadius: '8px' }}></Image>
                  <div className='content'>
                    <div className='text'>
                      <div className='name'>{item.novelName}</div>
                      <div className='updated'>最近更新:{item.latestUpdateChapter}</div>
                    </div>
                    <div className='number'>
                      <div className='chanpterCount'>{item.totalChapterCount} 章 | </div>
                      <div className='wordCount'>{item.totalWordCount} 字 |</div>
                      <div className=''>{item.isFinish == 0 ? '连载' : '未完结'}</div>
                    </div>
                  </div>


                </div>

              </Card>

            })
          }
          {/* <div className='item'>

          </div> */}

        </div>
        <div className='book-pagination'>
          <Pagination
            showSizeChanger
            onChange={handlePageChange}           // 页码改变
            onShowSizeChange={handleSizeChange}   // 页大小改变            
            pageSize={pageinfo.pageSize}
            total={pageinfo.total}
            current={pageinfo.currentPage}


          />

        </div>
      </Card>
      <Modal
        title="Basic Modal"
        closable={{ 'aria-label': 'Custom Close Button' }}
        open={isModalOpen}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <p>Some contents...</p>
        <p>Some contents...</p>
        <p>Some contents...</p>
      </Modal>
    </div>
  )

}

export default BookList

当然这里面使用的是 函数组件

现在写react 我感觉还是函数组件好使。简单 明了 类组件感觉不太好上手 需要详细查看才行

我这边使用的是ant-design 组件库 对这个react 框架 比较适配

我这边遇到的问题是 分页组件这里

复制代码
          <Pagination
            showSizeChanger
            onChange={handlePageChange}           // 页码改变
            onShowSizeChange={handleSizeChange}   // 页大小改变            
            pageSize={pageinfo.pageSize}
            total={pageinfo.total}
            current={pageinfo.currentPage}


          />

当然使用这个 ant-design,跟在vue中不太一样呢 用惯了vue 肯定不太习惯写react

复制代码
  const [pageinfo, setPageInfo] = useState({
    currentPage: 1,
    pageSize: 10,
    total: 0
  })

在react 中 这个东西就跟vue 的双向绑定 就不太一样 写法

复制代码
 setPageInfo(prev => ({
        ...prev,
        total: res.data.totalCount
      }))

赋值方法可定不一样

大家可以慢慢看 我来更新

相关推荐
lichenyang4531 小时前
从 Vite 空项目到 AIGC 图片工作台:我如何打通生图、任务轮询、生成库与 Canvas 动态特效
前端·人工智能
泡沫冰@1 小时前
上章节中文件的讲解
前端·网络·nginx
进击的丸子2 小时前
APP人脸识别增值版Harmony Demo实操与关键代码解析
前端·程序员·harmonyos
a1117762 小时前
唯美花朵风格的黑胶唱片音乐播放器
前端·css·css3
Hilaku2 小时前
工作 5 年后,决定你薪资上限的究竟是什么?
前端·javascript·程序员
爱分享的程序猿-Clark2 小时前
【前端分享】vue3 有 keep-alive属性吗?
前端
Revolution612 小时前
页面更新后为什么出现 Loading chunk failed:旧页面如何请求了已删除的构建产物
前端·面试·前端工程化
JavaGuide2 小时前
GitHub 9.8 万 Star!把整个代码仓库变成知识图谱,这个 AI Coding 工具太适合 Claude Code / Codex 了
前端·后端·ai编程
hunterandroid3 小时前
[鸿蒙从零到一] HarmonyOS 通知与提醒实战:消息发布、点击跳转与定时触达
前端
Lxinz3 小时前
vscode调试ts代码思路
前端