-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUseState.tsx
59 lines (57 loc) · 1.05 KB
/
UseState.tsx
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
48
49
50
51
52
53
54
55
56
57
58
59
import { useState } from 'react';
export default function UseState() {
const [count, setCount] = useState<number>(0);
const [value, setValue] = useState<number>(4711);
const [person, setPerson] = useState({
firstName: 'Thomas',
lastName: 'Scharke',
address: {
houseNumber: 42, // -> 23
street: '',
citiy: '',
},
});
return (
<div>
<p>
Current state of this Component: {value} | Person:
<code>{JSON.stringify(person)}</code>
</p>
<button
onClick={() => {
setCount(count + 1);
setValue(23);
setPerson({
...person,
address: {
...person.address,
houseNumber: 23,
street: 'xxx',
},
});
}}
>
Rest state!
</button>
<button
onClick={() => {
setValue(() => {
return 4712;
});
}}
>
Change state (primitive type)
</button>
<button
onClick={() => {
setValue((prevState) => {
console.log('Prev. State:', prevState);
return 4713;
});
}}
>
Change state (function)
</button>
</div>
);
}