1
+ /* HERE's how you create self contained components that also house render states */
1
2
import { useEffect , useState } from 'react'
2
3
import useGlobalStore from './../state/GlobalState'
3
4
import Skeleton from 'react-loading-skeleton'
@@ -13,22 +14,23 @@ export default function UserCard({
13
14
} : Tuser ) {
14
15
const [ isFollowing , setIsFollowing ] = useState ( false )
15
16
const auth = useGlobalStore ( ( state ) => state . auth )
16
- const { uid , user_following : currentUser_following } = useGlobalStore (
17
- ( state ) => state . user ,
18
- )
17
+ const { user_id : currentUser_id , user_following : currentUser_following } =
18
+ useGlobalStore ( ( state ) => state . user )
19
+
19
20
useEffect ( ( ) => {
21
+ // set the cards as isFollowing true first thing the component loads
22
+ // re-render when new data comes in or during follow/unfollow
20
23
const exists =
21
24
currentUser_following . find ( ( x ) => x === user_id ) == undefined
22
25
? false
23
26
: true
24
- // if(exists)
25
- console . log ( exists )
27
+ if ( auth && exists ) setIsFollowing ( true )
28
+ } , [ currentUser_following , user_id ] )
29
+ // IMPORTANT: Dont forget to re-render the components when the new data takes over the mock data
26
30
27
- // set the cards as isFollowing true first thing the component loads
28
- } , [ currentUser_following ] )
29
31
const handleClick = ( ) => {
30
32
// send to db
31
- setIsFollowing ( true )
33
+ // setIsFollowing(true)
32
34
}
33
35
return (
34
36
< article className = 'card mt-8' >
@@ -88,7 +90,7 @@ export default function UserCard({
88
90
) : (
89
91
< div className = 'group relative ms-auto' >
90
92
< button
91
- disabled = { auth && user_id == uid }
93
+ disabled = { auth && user_id == currentUser_id }
92
94
onClick = { handleClick }
93
95
className = 'inline-flex items-center gap-2 gap-x-1.5 rounded-full bg-accent-pink-500 px-3 py-2 text-xs font-medium text-white transition-all hover:bg-accent-pink-600 disabled:pointer-events-none disabled:opacity-50 dark:bg-accent-pink-900 sm:mt-1.5'
94
96
>
0 commit comments