You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: coding-exercise/README.md
+39
Original file line number
Diff line number
Diff line change
@@ -188,3 +188,42 @@ This issue can be fixed by wrapping the **<MyCustomInput />** component with `fo
188
188
189
189
**[⬆ Back to Top](#table-of-contents)**
190
190
191
+
#### 5. What is the outcome of number of clicks after 3 button clicks?
192
+
193
+
```javascript
194
+
import { useRef } from'react';
195
+
196
+
exportdefaultfunctionCounter() {
197
+
let ref =useRef(0);
198
+
199
+
functionhandleClick() {
200
+
ref.current=ref.current+1;
201
+
}
202
+
203
+
return (
204
+
<>
205
+
<div>Clicked + {ref.current} + times</div>
206
+
<button onClick={handleClick}>
207
+
Click me!
208
+
</button>
209
+
</>
210
+
);
211
+
}
212
+
```
213
+
214
+
- 1: 3 times
215
+
- 2: 4 times
216
+
- 3: 2 times
217
+
- 4: 0 times
218
+
219
+
<details><summary><b>Answer</b></summary>
220
+
<p>
221
+
222
+
##### Answer: 4
223
+
If you try to use **{ref.current}** in the render method, the number won’t be updated on click. This is because **ref.current** does not trigger a re-render unlike state. This property is mainly used to read and write the values inside event handler or outside the render method.
0 commit comments