-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-component.tsx
More file actions
47 lines (40 loc) · 1.03 KB
/
test-component.tsx
File metadata and controls
47 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React, { useState, useEffect } from 'react';
interface Props {
title: string;
count?: number;
}
const TestComponent: React.FC<Props> = ({ title, count = 0 }) => {
const [value, setValue] = useState<number>(count);
const [isVisible, setIsVisible] = useState<boolean>(true);
useEffect(() => {
console.log('Component mounted');
return () => {
console.log('Component unmounted');
};
}, []);
const handleClick = async (): Promise<void> => {
try {
setValue(prev => prev + 1);
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Value updated:', value);
} catch (error) {
console.error('Error:', error);
}
};
if (!isVisible) {
return null;
}
return (
<div className="container">
<h1>{title}</h1>
<p>Current value: {value}</p>
<button onClick={handleClick}>
Increment
</button>
<button onClick={() => setIsVisible(false)}>
Hide Component
</button>
</div>
);
};
export default TestComponent;