@@ -5,7 +5,8 @@ import React, {
5
5
createContext ,
6
6
useMemo ,
7
7
useContext ,
8
- useEffect
8
+ useEffect ,
9
+ useRef
9
10
} from 'react'
10
11
import mojs from 'mo-js'
11
12
import styles from './index.css'
@@ -120,15 +121,14 @@ const useClapAnimation = ({
120
121
return animationTimeline ;
121
122
}
122
123
123
- // 1. MediumClapContext lets us pass a value deep into
124
- // the tree of React components in MediumClap component
124
+ // MediumClapContext lets us pass a value deep into the tree of React components in MediumClap component
125
125
const MediumClapContext = createContext ( )
126
126
127
- // 2. Use a Provider to pass the current value to the tree below.
127
+ // Use a Provider to pass the current value to the tree below.
128
128
// Any component can read it, no matter how deep it is.
129
129
const { Provider } = MediumClapContext
130
130
131
- // 5. MediumClap component don’t know their children ahead of time.
131
+ // MediumClap component don’t know their children ahead of time.
132
132
// Use the special children prop to pass children elements directly into MediumClap
133
133
const MediumClap = ( { children, onClap } ) => {
134
134
const MAXIMUM_USER_CLAP = 50
@@ -149,8 +149,18 @@ const MediumClap = ({ children, onClap }) => {
149
149
clapCountTotalEl : clapCountTotalRef
150
150
} )
151
151
152
+ // 1. default value is false
153
+ // 1. set to true when MediumClap is rendered
154
+ const componentJustMounted = useRef ( false )
152
155
useEffect ( ( ) => {
153
- onClap && onClap ( clapState )
156
+ if ( componentJustMounted . current ) {
157
+ // 3. next time count changes
158
+ console . log ( 'onClap is called' )
159
+ onClap && onClap ( clapState )
160
+ } else {
161
+ // 2. set to true the first time in useEffect after rendered
162
+ componentJustMounted . current = true
163
+ }
154
164
} , [ count ] )
155
165
156
166
const handleClapClick = ( ) => {
@@ -165,7 +175,7 @@ const MediumClap = ({ children, onClap }) => {
165
175
} ) )
166
176
}
167
177
168
- // 3. Returns a memoized state.
178
+ // Returns a memoized state.
169
179
const memoizedValue = useMemo (
170
180
( ) => ( {
171
181
...clapState ,
@@ -174,9 +184,8 @@ const MediumClap = ({ children, onClap }) => {
174
184
)
175
185
176
186
return (
177
- // 4. Accepts a value prop to be passed to consuming components
178
- // that are descendants of this Provider.
179
- // 5. MediumClap component don’t know their children ahead of time.
187
+ // Accepts a value prop to be passed to consuming components that are descendants of this Provider.
188
+ // MediumClap component don’t know their children ahead of time.
180
189
// Use the special children prop to pass children elements directly into MediumClap
181
190
< Provider value = { memoizedValue } >
182
191
< button
@@ -192,7 +201,7 @@ const MediumClap = ({ children, onClap }) => {
192
201
}
193
202
194
203
const ClapIcon = ( ) => {
195
- // 6. Get prop from MediumClapContext instead from parent MediumClap
204
+ // Get prop from MediumClapContext instead from parent MediumClap
196
205
const { isClicked } = useContext ( MediumClapContext )
197
206
return (
198
207
< span >
@@ -209,7 +218,7 @@ const ClapIcon = () => {
209
218
}
210
219
211
220
const ClapCount = ( ) => {
212
- // 6. Get prop from MediumClapContext instead from parent MediumClap
221
+ // Get prop from MediumClapContext instead from parent MediumClap
213
222
const { count, setRef } = useContext ( MediumClapContext )
214
223
return (
215
224
< span
@@ -223,7 +232,7 @@ const ClapCount = () => {
223
232
}
224
233
225
234
const ClapCountTotal = ( ) => {
226
- // 6. Get prop from MediumClapContext instead from parent MediumClap
235
+ // Get prop from MediumClapContext instead from parent MediumClap
227
236
const { countTotal, setRef } = useContext ( MediumClapContext )
228
237
return (
229
238
< span
@@ -241,22 +250,27 @@ MediumClap.Count = ClapCount
241
250
MediumClap . Total = ClapCountTotal
242
251
// import MediumClap, { Icon, Count, Total } from 'medium-clap'
243
252
244
- // 5. MediumClap component don’t know their children ahead of time.
253
+ // MediumClap component don’t know their children ahead of time.
245
254
// Use the special children prop to pass children elements directly into MediumClap
246
255
const Usage = ( ) => {
247
256
// expose count, countTotal and isClicked
248
257
const [ count , setCount ] = useState ( 0 )
249
258
const handleClap = ( clapState ) => {
250
259
setCount ( clapState . count )
251
260
}
261
+ // count = 0
262
+ // !count = true
263
+ // !!count = false
252
264
return (
253
265
< div style = { { width : '100%' } } >
254
266
< MediumClap onClap = { handleClap } >
255
267
< MediumClap . Icon />
256
268
< MediumClap . Count />
257
269
< MediumClap . Total />
258
270
</ MediumClap >
259
- < div className = { styles . info } > { `You have clapped ${ count } ` } </ div >
271
+ { ! ! count && (
272
+ < div className = { styles . info } > { `You have clapped ${ count } times` } </ div >
273
+ ) }
260
274
</ div >
261
275
)
262
276
}
0 commit comments