-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComponentWithStateAndChildren.tsx
59 lines (50 loc) · 1.24 KB
/
ComponentWithStateAndChildren.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
/*
A component with children.
Toggles value on the level of the parent component and use
these modified values as props of the Child-Component.
*/
import { ReactElement, useState } from 'react';
export interface Person {
firstName: string;
lastName: string;
}
export const ComponentWithStateAndChildren = () => {
const [person, setPerson] = useState<Person>({
firstName: 'Thomas',
lastName: 'Scharke',
});
const toggleValues = () => {
setPerson(({ firstName, lastName }) => ({
firstName: lastName,
lastName: firstName,
}));
};
return (
<div>
<h3>ComponentWithStateAndChildren</h3>
<button onClick={toggleValues}>Toggle Values…</button>
<Child firstName={person.firstName} lastName={person.lastName}>
<div>Hello from Child</div>
</Child>
</div>
);
};
interface ChildProps {
firstName: string;
lastName: string;
children: ReactElement;
}
const Child = (props: ChildProps) => {
const { firstName, lastName, children } = props;
// const Comp = <div>jhe</div>;
return (
<div>
<h2>Child Component</h2>
<p>Diese Komponente zeigt die Daten der Person an…</p>
<div>Firstname: {firstName}</div>
<div>Lastname: {lastName}</div>
{children}
{/* <Comp foo="bar" bar="baz" />*/}
</div>
);
};