资讯专栏INFORMATION COLUMN

React+dva+webpack+antd-mobile 实战分享(二)

eternalshallow / 769人阅读

摘要:第一篇在上一篇文章中教给大家了怎么搭建项目的架子那么今天我们就来说一下项目里的导航和列表的实现导航废话不说啦下面直接给大家讲一下代码项目用的的框架应该没什么难度,我相信大家认真看文档的都能布局出来这个地方是封装的请求请求过来的数据全部存

第一篇 https://segmentfault.com/a/11...

在上一篇文章中教给大家了怎么搭建项目的架子;那么今天我们就来说一下项目里的导航和列表的实现

导航

废话不说啦 下面直接给大家讲一下代码
项目用的antd-mobile的框架 应该没什么难度,我相信大家认真看文档的都能布局出来;

TabButton.js

</>复制代码

  1. import React, { Component } from "react";
  2. import { Tabs, WhiteSpace,ListView,Toast} from "antd-mobile";
  3. import { routerRedux } from "dva/router";
  4. import { connect } from "dva";
  5. import Request from "../common/fetch"
  6. import {width,height} from "../common/style";
  7. const TabPane = Tabs.TabPane;
  8. class TabButton extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. channels: []
  13. }
  14. }
  15. componentDidMount() {
  16. // 这个地方是封装的fetch请求;
  17. Request("/api/article/channel",{
  18. secret:1111,
  19. },((res) => {
  20. this.setState({
  21. channels: res.result.channels
  22. })
  23. // 请求过来的数据全部存下来,以便后期调用,同时可以减少请求
  24. this.props.dispatch({
  25. type: "indexList/TabData",
  26. payload: res.result.channels,
  27. });
  28. }))
  29. }
  30. //这个点需要注意:此处是将click事件传递给子组件,另一界面 就可以取到此组件传递过去的click事件;
  31. _handleTabClick(key){
  32. this.props.ButtonClick(key);
  33. }
  34. _renderList() {
  35. let result = [];
  36. const channels = this.state.channels;
  37. for(let i in channels) {
  38. if(channels[i].attval == 1 || channels[i].attval == 2){
  39. result.push(
  40. )
  41. }
  42. }
  43. return result
  44. }
  45. _getMore() {
  46. this.props.dispatch(
  47. routerRedux.push("/moreChannel")
  48. )
  49. }
  50. render() {
  51. return(
  52. {this._handleTabClick(key)}}
  53. swipeable = {false}
  54. >
  55. {this._renderList()}
  56. this._getMore()}>

  57. )
  58. }
  59. }
  60. const styles = {
  61. moreChannel:{
  62. position:"absolute",
  63. top:0,
  64. right:-width/7,
  65. zIndex:9999,
  66. width:width/7,
  67. height:42,
  68. backgroundColor:"#fff",
  69. alignItems:"center",
  70. justifyContent:"center"
  71. }
  72. }
  73. function indexList({indexList}) {
  74. return { indexList };
  75. }
  76. export default connect(indexList)(TabButton);

fetch.js

</>复制代码

  1. export default function Request(url,body,callback){
  2. fetch(url,{
  3. method: "POST",
  4. mode: "cors",
  5. headers: {
  6. "Content-Type": "application/json",
  7. "Accept": "application/json"
  8. },
  9. body: JSON.stringify(body)
  10. }).then((res) => res.json()).then((res) => {
  11. callback(res)
  12. }).catch((err) => {
  13. console.log(err)
  14. })
  15. }
列表

indexTab.js

</>复制代码

  1. import React, { Component,PureComponent,PropTypes } from "react";
  2. import { Tabs, WhiteSpace,ListView,Toast} from "antd-mobile";
  3. import { routerRedux } from "dva/router";
  4. import { connect } from "dva";
  5. import ReactPullLoad,{ STATS } from "react-pullload";
  6. import TabButton from "./TabButton";
  7. import {width,height} from "../common/style";
  8. let devicenum = localStorage.getItem("devicenum")
  9. const loadMoreLimitNum = 10;
  10. const defaultStyle ={
  11. width: "100%",
  12. textAlign: "center",
  13. fontSize: "14px",
  14. lineHeight: "1.5",
  15. paddingTop:"12px",
  16. color:"#ccc"
  17. }
  18. class HeadNode extends PureComponent{
  19. static propTypes = {
  20. loaderState: PropTypes.string.isRequired,
  21. };
  22. static defaultProps = {
  23. loaderState: STATS.init,
  24. };
  25. render(){
  26. const {
  27. loaderState
  28. } = this.props
  29. let content = ""
  30. if(loaderState == STATS.pulling){
  31. content = "下拉刷新"
  32. } else if(loaderState == STATS.enough){
  33. content = "松开刷新"
  34. } else if(loaderState == STATS.refreshing){
  35. content = "正在刷新..."
  36. } else if(loaderState == STATS.refreshed){
  37. content = "刷新成功"
  38. }
  39. return(
  40. {content}
  41. )
  42. }
  43. }
  44. class FooterNode extends PureComponent{
  45. static propTypes = {
  46. loaderState: PropTypes.string.isRequired,
  47. hasMore: PropTypes.bool.isRequired
  48. };
  49. static defaultProps = {
  50. loaderState: STATS.init,
  51. hasMore: true
  52. };
  53. render(){
  54. const {
  55. loaderState,
  56. hasMore
  57. } = this.props
  58. let content = ""
  59. if(loaderState == STATS.loading){
  60. return(
  61. 正在加載喔~
  62. )
  63. } else if(hasMore === false){
  64. content = "没有更多"
  65. }
  66. return(
  67. {content}
  68. )
  69. }
  70. }
  71. class indexTab extends Component {
  72. constructor(props) {
  73. super(props)
  74. this.state = {
  75. channels : [],
  76. channelid : 1,
  77. showT:false,
  78. loading : false,
  79. hasMore: true,
  80. data: [],
  81. action: STATS.init,
  82. index: loadMoreLimitNum,
  83. newsLength:""
  84. }
  85. }
  86. componentDidMount() {
  87. this.getListData(this.state.channelid);
  88. }
  89. getListData(channelid) {
  90. // List
  91. fetch("/api/article",{
  92. method: "POST",
  93. mode: "cors",
  94. headers: {
  95. "Content-Type": "application/json",
  96. "Accept": "application/json"
  97. },
  98. body: JSON.stringify({
  99. channelID: channelid,
  100. type: 0,
  101. pageSize: 10,
  102. dt : 2,
  103. action: 1,
  104. devicenum:devicenum
  105. })
  106. }).then((res) => res.json()).then((res) => {
  107. this.setState({
  108. data: res.result.news,
  109. newsLength:res.result.news.length
  110. })
  111. this.props.dispatch({
  112. type: "indexList/detailData",
  113. payload: res.result.news,
  114. });
  115. }).then(() => {
  116. setTimeout(() => {
  117. this.setState({
  118. showT : true
  119. })
  120. },1900)
  121. }).then(() => {
  122. setTimeout(() => {
  123. this.setState({
  124. showT : false
  125. })
  126. },2900)
  127. }).catch((err) => {
  128. console.log(err)
  129. })
  130. }
  131. handleAction = (action) => {
  132. console.info(action, this.state.action,action === this.state.action);
  133. if(action === this.state.action){
  134. return false
  135. }
  136. if(action === STATS.refreshing){//刷新
  137. this.handRefreshing();
  138. } else if(action === STATS.loading){
  139. this.handLoadMore();
  140. } else{
  141. this.setState({
  142. action: action
  143. })
  144. }
  145. }
  146. handRefreshing = () =>{
  147. if(STATS.refreshing === this.state.action){
  148. return false
  149. }
  150. this.getListData(this.state.channelid)
  151. setTimeout(()=>{
  152. this.setState({
  153. action: STATS.refreshed,
  154. index: loadMoreLimitNum
  155. });
  156. }, 3000)
  157. }
  158. handLoadMore = () => {
  159. if(STATS.loading === this.state.action){
  160. return false
  161. }
  162. setTimeout(()=>{
  163. if(this.state.index === 0){
  164. this.setState({
  165. action: STATS.reset,
  166. hasMore: false
  167. });
  168. } else{
  169. fetch("/api/article",{
  170. method: "POST",
  171. headers: {
  172. "Content-Type": "application/json;charset=UTF-8",
  173. "Accept": "application/json"
  174. },
  175. body: JSON.stringify({
  176. channelID: this.state.channelid,
  177. type: 0,
  178. pageSize: 10,
  179. dt : 2,
  180. action: 1,
  181. devicenum:devicenum
  182. })
  183. }).then((res) => res.json()).then((res) => {
  184. this.setState({
  185. data: [...this.state.data,...res.result.news],
  186. action: STATS.reset,
  187. index: this.state.index - 1
  188. })
  189. this.props.dispatch({
  190. type: "indexList/detailData",
  191. payload: [...this.state.data,...res.result.news],
  192. });
  193. }).then(() => {
  194. console.log(this.state.showT)
  195. setTimeout(() => {
  196. this.setState({
  197. showT : true
  198. })
  199. },1900)
  200. }).then(() => {
  201. setTimeout(() => {
  202. this.setState({
  203. showT : false
  204. })
  205. },2900)
  206. }).catch((err) => {
  207. console.log(err)
  208. })
  209. }
  210. }, 3000)
  211. this.setState({
  212. action: STATS.loading
  213. })
  214. }
  215. //跳转到详情页
  216. _routerDetail(index) {
  217. localStorage.setItem("detailid",index)
  218. this.props.dispatch(
  219. routerRedux.push(`/detail/${index}`)
  220. )
  221. }
  222. //Tab 切换重新调取
  223. ButtonClick(key) {
  224. this.getListData(key);
  225. this.setState({
  226. channelid:key
  227. })
  228. }
  229. _renderShow() {
  230. if(this.state.showT == true){
  231. if(this.state.newsLength != 0){
  232. return(
  233. 更新了{this.state.newsLength}条内容

  234. )
  235. }else{
  236. return(
  237. 暂無更新推送

  238. )
  239. }
  240. }else{
  241. return(
  242. )
  243. }
  244. }
  245. render(){
  246. const {data,hasMore} = this.state
  247. return (
    • {
    • data.map( (str, index )=>{
    • if(str.images[0] != ""){
    • return
    • this._routerDetail(index)}>
    • {str.title}

    • {str.source} | {str.publishTime}

    • }else{
    • return
    • this._routerDetail(index)}>
    • {str.title}

    • {str.source} | {str.publishTime}

    • }
    • })
    • }
  248. {this._renderShow()}
  249. )
  250. }
  251. }
  252. const styles = {
  253. more: {
  254. width:width,
  255. backgroundColor:"#FFDB01",
  256. position:"absolute",
  257. zIndex:9999,
  258. top:86,
  259. textAlign:"center",
  260. padding:5,
  261. fontSize:14,
  262. display:"block",
  263. },
  264. news: {
  265. padding:15,
  266. justifyContent:"center",
  267. alignItems:"center"
  268. },
  269. imgStyle: {
  270. width:width-30,
  271. //height:100
  272. },
  273. newsTitle: {
  274. fontSize:18,
  275. marginTop:10,
  276. marginBottom:10
  277. },
  278. moreTab: {
  279. width:width-(width/7)*6,
  280. height:43,
  281. backgroundColor:"#fff",
  282. position: "absolute",
  283. justifyContent:"center",
  284. alignItems:"center",
  285. top:44,
  286. right:0,
  287. zIndex:9999
  288. }
  289. }
  290. function indexList({ indexList }) {
  291. return { indexList };
  292. }
  293. export default connect(indexList)(indexTab);

好啦 上述就是整个首页的主要代码,知道如何创建项目的你们可以尝试啦~~~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/83900.html

相关文章

  • React+dva+webpack+antd-mobile 实战分享(一)

    摘要:再看本篇文章之前,本人还是建议想入坑的童鞋可以选有来创建的项目,因为现在和还不成熟,坑相对要多一些,当然如果你已经做好跳坑的准备,那么请继续往下走本文适合对有一定了解的人。 再看本篇文章之前,本人还是建议想入坑react的童鞋可以选有create-react-app来创建react的项目,因为现在dva和roadhog还不成熟,坑相对要多一些,当然如果你已经做好跳坑的准备,那么请继续往...

    ziwenxie 评论0 收藏0
  • 使用Dva+antd-mobile构建一个移动端web

    摘要:整个应用的,由多个小的的以为合成该当前的状态。执行异步函数发出一个,类似于取的值通过函数将需要用到的数据合并到中,再在组件中取修改的值通过被的会自动在中拥有方法请求统一讲用到所有的接口放到中方便管理函数柯里化 项目地址:dva-demo 随手一个小star给予动力,谢谢! Build Setup # install dependencies npm install # serve ...

    Jaden 评论0 收藏0

发表评论

0条评论

eternalshallow

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<