Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitdd874db

Browse files
committed
refactor(FollowButton): change to hooks version
1 parent299b39c commitdd874db

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

‎src/components/FollowButton/FollowingBtn.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const FollowingBtn = ({ size, loading, onClick }) => {
1717
</BtnWrapper>
1818
</Button>
1919
) :(
20-
<Tooltipplacement="bottom"content={<Popinfo>点击取消关注</Popinfo>}>
20+
<Tooltip
21+
placement="bottom"
22+
delay={800}
23+
content={<Popinfo>点击取消关注</Popinfo>}
24+
>
2125
<Buttonsize={size}type="primary"ghostonClick={onClick}>
2226
<BtnWrapper>
2327
<WatchedIconsrc={`${ICON_CMD}/check2.svg`}/>

‎src/components/FollowButton/index.js‎

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
*/
66

7-
importReactfrom'react'
7+
importReact,{useState,useCallback}from'react'
88
importTfrom'prop-types'
99

1010
import{buildLog}from'@/utils'
@@ -14,57 +14,47 @@ import FollowBtn from './FollowBtn'
1414
/* eslint-disable-next-line */
1515
constlog=buildLog('c:FollowButton:index')
1616

17-
classFollowButtonextendsReact.Component{
18-
state={simuLoading:false}
19-
20-
onFollow(){
21-
const{ userId, onFollow, fakeLoading}=this.props
17+
constFollowButton=({
18+
hasFollowed,
19+
size,
20+
loading,
21+
userId,
22+
fakeLoading,
23+
onFollow,
24+
onUndoFollow,
25+
})=>{
26+
const[simuLoading,setSimuLoading]=useState(false)
27+
constisLoading=fakeLoading ?simuLoading :loading
2228

29+
consthandleFollow=useCallback(()=>{
2330
if(fakeLoading){
24-
this.doFakeLoading()
31+
setSimuLoading(true)
32+
setTimeout(()=>setSimuLoading(false),1000)
2533
}
2634
onFollow(userId)
27-
}
28-
29-
onUndoFollow(){
30-
const{ userId, onUndoFollow, fakeLoading}=this.props
35+
},[fakeLoading,onFollow,userId])
3136

37+
consthandleUndoFollow=useCallback(()=>{
3238
if(fakeLoading){
33-
this.doFakeLoading()
39+
setSimuLoading(true)
40+
setTimeout(()=>setSimuLoading(false),1000)
3441
}
3542
onUndoFollow(userId)
36-
}
37-
38-
doFakeLoading(){
39-
this.setState({simuLoading:true})
40-
setTimeout(()=>{
41-
this.setState({simuLoading:false})
42-
},1000)
43-
}
44-
45-
render(){
46-
const{ hasFollowed, size, loading, fakeLoading}=this.props
47-
const{ simuLoading}=this.state
48-
constisLoading=fakeLoading ?simuLoading :loading
43+
},[fakeLoading,onUndoFollow,userId])
4944

50-
return(
51-
<React.Fragment>
52-
{hasFollowed ?(
53-
<FollowingBtn
54-
size={size}
55-
loading={isLoading}
56-
onClick={()=>this.onUndoFollow()}
57-
/>
58-
) :(
59-
<FollowBtn
60-
size={size}
61-
loading={isLoading}
62-
onClick={()=>this.onFollow()}
63-
/>
64-
)}
65-
</React.Fragment>
66-
)
67-
}
45+
return(
46+
<React.Fragment>
47+
{hasFollowed ?(
48+
<FollowingBtn
49+
size={size}
50+
loading={isLoading}
51+
onClick={handleUndoFollow}
52+
/>
53+
) :(
54+
<FollowBtnsize={size}loading={isLoading}onClick={handleFollow}/>
55+
)}
56+
</React.Fragment>
57+
)
6858
}
6959

7060
FollowButton.propTypes={

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp