本文主要内容是制作文章列表,实现流程与之前基本相同。因此,在此填写主要的组件、样式和 reducer 代码。
1. 文章列表制作
1.1 List 组件
import React, { Component } from 'react';
import { ListInfo, ListItem } from '../style';
import { connect } from 'react-redux';
class List extends Component {
render() {
const { list } = this.props;
return (
<div>
{list.map((item) => (
<ListItem key={item.get('id')}>
<img className='pic' src={item.get('imgUrl')} alt='' />
<ListInfo>
<h3 className='title'>{item.get('title')}</h3>
<p className='desc'>{item.get('desc')}</p>
</ListInfo>
</ListItem>
))}
</div>
);
}
}
const mapStateToProps = (state) => ({
list: state.getIn(['home', 'articleList']),
});
export default connect(mapStateToProps, null)(List);
1.2 样式
jsxCopy code
export const ListItem = styled.div`
overflow: hidden;
padding: 20px 0;
border-bottom: 1px solid #dcdcdc;
.pic {
display: block;
width: 125px;
height: 100px;
float: right;
}
`;
export const ListInfo = styled.div`
width: 500px;
float: left;
.title {
line-height: 27px;
font-size: 18px;
font-weight: bold;
color: #333;
}
.desc {
line-height: 24px;
font-size: 13px;
color: #999;
}
`;
1.3 reducer
const defaultState = fromJS({
topicList: [
{
id: 1,
title: '社会热点',
imgUrl:
'https://upload-images.jianshu.io/upload_images/1741702-7d340c9913d5ada8.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/424/format/webp',
},
{
id: 2,
title: '手绘',
imgUrl:
'https://upload-images.jianshu.io/upload_images/1741702-7d340c9913d5ada8.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/424/format/webp',
},
],
articleList:[{ id:1, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' },{ id:2, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' },{ id:3, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' }]
});
2. 推荐区域
import React, { Component } from 'react';
import { ListInfo, ListItem } from '../style';
import { connect } from 'react-redux';
class List extends Component {
render() {
const { list } = this.props;
return (
<div>
{list.map((item) => (
<ListItem key={item.get('id')}>
<img className='pic' src={item.get('imgUrl')} alt='' />
<ListInfo>
<h3 className='title'>{item.get('title')}</h3>
<p className='desc'>{item.get('desc')}</p>
</ListInfo>
</ListItem>
))}
</div>
);
}
}
const mapStateToProps = (state) => ({
list: state.getIn(['home', 'articleList']),
});
export default connect(mapStateToProps, null)(List);
jsxCopy code
export const ListItem = styled.div`
overflow: hidden;
padding: 20px 0;
border-bottom: 1px solid #dcdcdc;
.pic {
display: block;
width: 125px;
height: 100px;
float: right;
}
`;
export const ListInfo = styled.div`
width: 500px;
float: left;
.title {
line-height: 27px;
font-size: 18px;
font-weight: bold;
color: #333;
}
.desc {
line-height: 24px;
font-size: 13px;
color: #999;
}
`;
const defaultState = fromJS({
topicList: [
{
id: 1,
title: '社会热点',
imgUrl:
'https://upload-images.jianshu.io/upload_images/1741702-7d340c9913d5ada8.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/424/format/webp',
},
{
id: 2,
title: '手绘',
imgUrl:
'https://upload-images.jianshu.io/upload_images/1741702-7d340c9913d5ada8.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/424/format/webp',
},
],
articleList:[{ id:1, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' },{ id:2, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' },{ id:3, title:'Python大佬实战APP抓包,抖音的小姐姐等着我!', desc:'APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取。现在手机 App 用的越来越多,...', imgUrl:'https://upload-images.jianshu.io/upload_images/13448242-20871e9f865fb242.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/594/format/webp' }]
});
这部分编写逻辑和上面都是类似的,就不贴出对应代码了