前端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
      }))

赋值方法可定不一样

大家可以慢慢看 我来更新

相关推荐
Kagol5 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路6 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide7 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter7 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸8 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000008 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉8 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
兆子龙9 小时前
从高阶函数到 Hooks:React 如何减轻开发者的心智负担(含 Demo + ahooks 推荐)
前端
狗胜9 小时前
测试文章 - API抓取
前端