实现思路

展示数据

数据没加载出来 : props 数据没传递过来 没有文章

分页

有文章 :非空数组 文章数据 / [条数<= 10 不需要做分页] 文章数据 / [条数 >10 需要做分页]

ArticleItem组件

样式布局

上: 左 作者头像 / 作者名称 文章创建时间 右: 喜欢 下: 左 标题 / 描述 右 : 标签

数据参考

{ “slug”: “1371cdf8”, “title”: “你好aa”, “description”: “html 你好aa”, “body”: “html 你好aa”, “createdAt”: “2022-01-20T05:28:57.000Z”, “updatedAt”: “2022-01-20T05:28:57.000Z”, “userEmail”: “lisi@qq.com”, “tags”: [ “html”, “tag1” ], “author”: { “username”: “lisi”, “email”: “lisi@qq.com”, “bio”: null, “avatar”: null }, “isFavorite”: false, “favoriteCount”: 1 },

代码实现

  1. const FAVORITED_CLASS = 'btn btn-sm btn-primary';
  2. const NOT_FAVORITED_CLASS = 'btn btn-sm btn-outline-primary';
  3. const ArticleItem = ({article})=>{
  4. return (
  5. <div className="article-preview">
  6. {/* 上 基础信息 */}
  7. <div className='article-meta'>
  8. {/* 头像 */}
  9. <Link to={`/${article.author.username}`}>
  10. <img src={article.author.avatar || 'https://yudafeng.github.io/static/default.png'} alt={article.author.username}/>
  11. </Link>
  12. {/* 作者名称 文章创建时间 */}
  13. <div className='info'>
  14. {/* 作者姓名 */}
  15. <Link to={`/${article.author.username}`}>
  16. {article.author.username}
  17. </Link>
  18. {/* 文章创建时间 */}
  19. <span>
  20. {new Date(article.createdAt).toDateString()}
  21. </span>
  22. </div>
  23. <div className="pull-xs-right">
  24. {/* class 类样式: 喜欢 和 不喜欢 */}
  25. <button className={article.isFavorite ? FAVORITED_CLASS :NOT_FAVORITED_CLASS }>
  26. <i class="fa fa-heart-o"></i>{article.favoriteCount}
  27. </button>
  28. </div>
  29. </div>
  30. {/* 下 */}
  31. {/* 文章简介 */}
  32. <Link to={`/article/${article.slug}`} className="preview-link">
  33. <h1>{article.title}</h1>
  34. <p>{article.description}</p>
  35. <span>阅读更多....</span>
  36. {/* 标签 */}
  37. <ul className="tag-list">
  38. {
  39. article.tags.map(tag=>{
  40. return (
  41. <li className="tag-default tag-pill tag-outline" key={tag}>{tag}</li>
  42. )
  43. })
  44. }
  45. </ul>
  46. </Link>
  47. </div>
  48. )
  49. }
  50. export default ArticleItem

pagination分页组件

编码实现

  1. import {memo} from 'react'
  2. const Pagination = props =>{
  3. // props : 文章总条数 count / 当前第几页 current
  4. // [条数<= 10 不需要做分页]
  5. if(props.count<=10){
  6. return null
  7. }
  8. // [条数 >10 需要做分页]
  9. // 1) 条数 =>页数
  10. const pageNums = []
  11. for (let index = 1; index <=Math.ceil(props.count/10); index++) {
  12. pageNums.push(index)
  13. }
  14. //2) 构建分页组件内容
  15. return (
  16. <nav>
  17. <ul>
  18. {
  19. pageNums.map(pageNum=>{
  20. //当前页
  21. const isCurrentPage = pageNum ==props.current
  22. //页码
  23. return (
  24. <li key={pageNum} className={isCurrentPage?'page-item active' : 'page-item'}>
  25. <button type='button' className='page-link'>{pageNum}</button>
  26. </li>
  27. )
  28. })
  29. }
  30. </ul>
  31. </nav>
  32. )
  33. }
  34. export default memo(Pagination)

index主页面组件

编码实现

  1. import {memo} from 'react'
  2. import Pagination from './Pagination'
  3. import ArticleItem from './ArticleItem'
  4. /**
  5. * 文章列表
  6. * props: 属性名articles [{article1},{article2}..] / 分页数据
  7. */
  8. const ArticlesList= props =>{
  9. //数据没加载出来 : props 数据没传递过来
  10. if(!props.articles){
  11. return (
  12. <div className='article-preview'>加载中...</div>
  13. )
  14. }
  15. //没有文章 : 空数组
  16. if(props.articles.length===0){
  17. return (
  18. <div className='article-preview'>这里还没有文章</div>
  19. )
  20. }
  21. // 有文章 :非空数组
  22. // 文章数据 / [条数<= 10 不需要做分页]
  23. // 文章数据 / [条数 >10 需要做分页]
  24. return (
  25. <div>
  26. {/* 文章数据 */}
  27. {
  28. props.articles.map(article=>{
  29. return (
  30. <ArticleItem article={article} key={article.slug}/>
  31. )
  32. })
  33. }
  34. {/* 分页 */}
  35. <Pagination
  36. count={props.articlesCount}
  37. current={props.currenPage}
  38. />
  39. </div>
  40. )
  41. }
  42. export default memo(ArticlesList)