Skip to content

Commit 444b600

Browse files
committed
feat: Add final documentation.
1 parent 904fb85 commit 444b600

File tree

1 file changed

+135
-0
lines changed
  • documentation/08-uis/03-state-und-struktur

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# State und Struktur
2+
3+
- Wie baut man Listen in Web-Components dynamisch auf?
4+
5+
```javascript
6+
renderTodo(todo) {
7+
return html`
8+
<tnw-todo>${todo.title}</tnw-todo>
9+
`;
10+
}
11+
12+
render() {
13+
const todos = [
14+
// ...
15+
];
16+
17+
if (todos.length === 0) {
18+
return html`
19+
<div>Keine To-Dos vorhanden</div>
20+
`;
21+
}
22+
23+
return html`
24+
<tnw-todos>
25+
${todos.map(todo => renderTodo(todo))}
26+
</tnw-todos>
27+
`;
28+
}
29+
```
30+
31+
- State
32+
- State = Daten, die sich ändern
33+
- Änderungen am State erzwingen ein Re-Rendering
34+
- Unterschied zu Properties
35+
- Properties werden von Außen in eine Komponente hineingegeben, read-only
36+
- State wird intern verwaltet, änderbar
37+
- EVA-Prinzip
38+
- Props => Eingabe
39+
- State => Verarbeitung
40+
- Events => Ausgabe
41+
- Wo gehört State hin? => State-Management
42+
43+
- Re-Rendering
44+
- Erfolgt automatisch in Lit, sobald sich eine `@property` ändert
45+
- `@state` verwenden für interne Variablen statt `@property`
46+
47+
- Wo gehört State hin?
48+
- "Lift state up" => State so weit oben wie möglich unterbringen
49+
- State, der möglichst weit "oben" sitzt, ist eine Single-Source of Truth
50+
51+
- Zwei Arten von Komponenten
52+
- Presentational Component
53+
- Daten anzeigen
54+
- Nur Props, kein State
55+
- Ggf. bei Eingaben Events auslösen
56+
- Wiederverwendbar und testbar
57+
- Container Component
58+
- Hat State
59+
- Daten laden und ggf. zu speichern, zB über eine API
60+
- Führt Geschäftslogik aus
61+
- Verwaltet anwendungsrelevanten Daten
62+
- Koordiniert Presentational Components
63+
- Viele Presentational Components, wenig Container Components
64+
65+
- State-Management
66+
67+
```javascript
68+
render() {
69+
const todos = [];
70+
71+
return <App todos={todos} />
72+
}
73+
74+
class App {
75+
render(todos) {
76+
return <TodoList todos={todos} />
77+
}
78+
}
79+
80+
class TodoList {
81+
render(todos) {
82+
return <Todo todo={...}>
83+
}
84+
}
85+
```
86+
87+
```html
88+
const todos=...
89+
90+
<tnw-app>
91+
<tnw-todo-list>
92+
${todos.map =>
93+
<tnw-todo-item text=${todo.title}></tnw-todo-item>
94+
}
95+
</tnw-todo-list>
96+
</tnw-app>
97+
```
98+
99+
- Fachliche vs technische Struktur im Frontend
100+
101+
```
102+
models/
103+
AuthModel.js
104+
CartModel.js
105+
controllers/
106+
AuthController.js
107+
CartController.js
108+
views/
109+
AuthView.js
110+
AuthView.html
111+
AuthView.css
112+
CartView.js
113+
CartView.html
114+
CartView.css
115+
```
116+
117+
vs
118+
119+
```
120+
shared/
121+
database.js
122+
domain/
123+
auth/
124+
Model.js
125+
Controller.js
126+
View.js
127+
View.html
128+
View.css
129+
cart/
130+
Model.js
131+
Controller.js
132+
View.js
133+
View.html
134+
View.css
135+
```

0 commit comments

Comments
 (0)