1- import React , { PureComponent } from 'react' ;
1+ import React , { useRef , useEffect } from 'react' ;
22import Horizen from '../../baseUI/horizen-item/index' ;
33import { categoryTypes , alphaTypes } from '../../api/config' ;
44import {
@@ -15,46 +15,53 @@ import LazyLoad, {forceCheck} from 'react-lazyload';
1515import Loading from '../../baseUI/loading/index' ;
1616import { renderRoutes } from 'react-router-config' ;
1717
18- // 这里采用class,之前写过hooks版,但是pageCount的更新存在bug,
19- // 有兴趣的小伙伴可以试试改为hooks版本, bug修复后换成函数式,欢迎大家提pr修正
20- class Singers extends PureComponent {
21- constructor ( props ) {
22- super ( props ) ;
23- Singers . scrollChild = React . createRef ( null ) ;
24- }
25- componentDidMount ( ) {
26- if ( ! this . props . singerList . length && ! this . props . category && ! this . props . alpha ) {
27- this . props . getHotSinger ( ) ;
18+ function Singers ( props ) {
19+ const scrollRef = useRef ( null ) ;
20+
21+ const { singerList , category , alpha , pageCount , songsCount , pullUpLoading , pullDownLoading , enterLoading } = props ;
22+
23+ const { getHotSinger , updateCategory , updateAlpha , pullUpRefresh , pullDownRefresh } = props ;
24+
25+ useEffect ( ( ) => {
26+ if ( ! singerList . length && ! category && ! alpha ) {
27+ getHotSinger ( ) ;
2828 }
29- }
29+ // eslint-disable-next-line
30+ } , [ ] ) ;
3031
31- enterDetail ( id ) {
32- this . props . history . push ( `/singers/${ id } ` ) ;
33- }
34- handlePullUp ( ) {
35- this . props . pullUpRefresh ( this . props . category === '' , this . props . pageCount ) ;
36- }
37- handlePullDown ( ) {
38- this . props . pullDownRefresh ( this . props . category , this . props . pageCount ) ;
39- }
40- handleUpdateCategory ( newVal ) {
41- if ( this . props . category === this . props . newVal ) return ;
42- this . props . updateCategory ( newVal ) ;
43- }
32+ const enterDetail = ( id ) => {
33+ props . history . push ( `/singers/${ id } ` ) ;
34+ } ;
4435
45- handleUpdateAlpha ( newVal ) {
46- if ( this . props . alpha === newVal ) return ;
47- this . props . updateAlpha ( newVal ) ;
48- }
49- renderSingerList ( ) {
50- const { singerList} = this . props ;
36+ const handlePullUp = ( ) => {
37+ pullUpRefresh ( category === '' , pageCount ) ;
38+ } ;
39+
40+ const handlePullDown = ( ) => {
41+ pullDownRefresh ( category , pageCount ) ;
42+ } ;
43+
44+ const handleUpdateCategory = ( newVal ) => {
45+ if ( category === newVal ) return ;
46+ scrollRef . current . refresh ( ) ;
47+ updateCategory ( newVal ) ;
48+ } ;
49+
50+ const handleUpdateAlpha = ( newVal ) => {
51+ if ( alpha === newVal ) return ;
52+ scrollRef . current . refresh ( ) ;
53+ updateAlpha ( newVal ) ;
54+ } ;
55+
56+ const renderSingerList = ( ) => {
57+ const { singerList} = props ;
5158
5259 return (
5360 < List >
5461 {
5562 singerList . toJS ( ) . map ( ( item , index ) => {
5663 return (
57- < ListItem key = { item . accountId + "" + index } onClick = { ( ) => this . enterDetail ( item . id ) } >
64+ < ListItem key = { item . accountId + "" + index } onClick = { ( ) => enterDetail ( item . id ) } >
5865 < div className = "img_wrapper" >
5966 < LazyLoad placeholder = { < img width = "100%" height = "100%" src = { require ( './singer.png' ) } alt = "music" /> } >
6067 < img src = { `${ item . picUrl } ?param=300x300` } width = "100%" height = "100%" alt = "music" />
@@ -67,35 +74,31 @@ class Singers extends PureComponent{
6774 }
6875 </ List >
6976 )
70- }
71- render ( ) {
72- const { alpha, category, enterLoading, songsCount, pullUpLoading, pullDownLoading } = this . props ;
73-
74- return (
75- < div >
76- { /* 对于better-scroll来讲,其作用的元素外面必须要有一个尺寸确定的容器包裹,因此设置xxxContainer */ }
77- < NavContainer >
78- < Horizen title = { "分类(默认热门):" } list = { categoryTypes } handleClick = { ( v ) => this . handleUpdateCategory ( v ) } oldVal = { category } > </ Horizen >
79- < Horizen title = { "首字母:" } list = { alphaTypes } handleClick = { ( v ) => this . handleUpdateAlpha ( v ) } oldVal = { alpha } > </ Horizen >
80- </ NavContainer >
81- < ListContainer play = { songsCount } >
82- < Scroll
83- onScroll = { ( ) => forceCheck ( ) }
84- pullUp = { ( ) => this . handlePullUp ( ) }
85- pullDown = { ( ) => this . handlePullDown ( ) }
86- ref = { Singers . scrollChild }
87- pullUpLoading = { pullUpLoading }
88- pullDownLoading = { pullDownLoading }
89- >
90- { this . renderSingerList ( ) }
91- </ Scroll >
92- </ ListContainer >
93- { /* 入场加载动画 */ }
94- { enterLoading ? < EnterLoading > < Loading > </ Loading > </ EnterLoading > : null }
95- { renderRoutes ( this . props . route . routes ) }
96- </ div >
97- )
98- }
77+ } ;
78+ return (
79+ < div >
80+ { /* 对于better-scroll来讲,其作用的元素外面必须要有一个尺寸确定的容器包裹,因此设置xxxContainer */ }
81+ < NavContainer >
82+ < Horizen title = { "分类(默认热门):" } list = { categoryTypes } handleClick = { ( v ) => handleUpdateCategory ( v ) } oldVal = { category } > </ Horizen >
83+ < Horizen title = { "首字母:" } list = { alphaTypes } handleClick = { ( v ) => handleUpdateAlpha ( v ) } oldVal = { alpha } > </ Horizen >
84+ </ NavContainer >
85+ < ListContainer play = { songsCount } >
86+ < Scroll
87+ onScroll = { ( ) => forceCheck ( ) }
88+ pullUp = { handlePullUp }
89+ pullDown = { handlePullDown }
90+ ref = { scrollRef }
91+ pullUpLoading = { pullUpLoading }
92+ pullDownLoading = { pullDownLoading }
93+ >
94+ { renderSingerList ( ) }
95+ </ Scroll >
96+ </ ListContainer >
97+ { /* 入场加载动画 */ }
98+ { enterLoading ? < EnterLoading > < Loading > </ Loading > </ EnterLoading > : null }
99+ { renderRoutes ( props . route . routes ) }
100+ </ div >
101+ )
99102}
100103const mapStateToProps = ( state ) => ( {
101104 alpha : state . getIn ( [ 'singers' , 'alpha' ] ) ,
@@ -113,14 +116,12 @@ const mapDispatchToProps = (dispatch) => {
113116 dispatch ( getHotSingerList ( ) ) ;
114117 } ,
115118 updateCategory ( newVal ) {
116- Singers . scrollChild . current . refresh ( ) ;
117119 dispatch ( changeCategory ( newVal ) ) ;
118120 dispatch ( changePageCount ( 0 ) ) ;
119121 dispatch ( changeEnterLoading ( true ) ) ;
120122 dispatch ( getSingerList ( ) ) ;
121123 } ,
122124 updateAlpha ( newVal ) {
123- Singers . scrollChild . current . refresh ( ) ;
124125 dispatch ( changeAlpha ( newVal ) ) ;
125126 dispatch ( changePageCount ( 0 ) ) ;
126127 dispatch ( changeEnterLoading ( true ) ) ;
0 commit comments