|
6 | 6 | - [State](#state)
|
7 | 7 | - [Closure component state](#closure-component-state)
|
8 | 8 | - [POJO component state](#pojo-component-state)
|
9 |
| -- [ES6 Classes](#es6-classes) |
| 9 | +- [Classes](#classes) |
10 | 10 | - [Class component state](#class-component-state)
|
| 11 | +- [Special attributes](#special-attributes) |
11 | 12 | - [Avoid anti-patterns](#avoid-anti-patterns)
|
12 | 13 |
|
13 | 14 | ### Structure
|
@@ -115,7 +116,7 @@ Note that unlike many other frameworks, mutating component state does *not* trig
|
115 | 116 |
|
116 | 117 | If a state change occurs that is not as a result of any of the above conditions (e.g. after a `setTimeout`), then you can use `m.redraw()` to trigger a redraw manually.
|
117 | 118 |
|
118 |
| -#### Closure Component State |
| 119 | +#### Closure component state |
119 | 120 |
|
120 | 121 | In the above examples, each component is defined as a POJO (Plain Old JavaScript Object), which is used by Mithril internally as the prototype for that component's instances. It's possible to use component state with a POJO (as we'll discuss below), but it's not the cleanest or simplest approach. For that we'll use a **_closure component_**, which is simply a wrapper function which _returns_ a POJO component instance, which in turn carries its own, closed-over scope.
|
121 | 122 |
|
@@ -183,7 +184,7 @@ A big advantage of closure components is that we don't need to worry about bindi
|
183 | 184 |
|
184 | 185 | ---
|
185 | 186 |
|
186 |
| -#### POJO Component State |
| 187 | +#### POJO component state |
187 | 188 |
|
188 | 189 | It is generally recommended that you use closures for managing component state. If, however, you have reason to manage state in a POJO, the state of a component can be accessed in three ways: as a blueprint at initialization, via `vnode.state` and via the `this` keyword in component methods.
|
189 | 190 |
|
@@ -247,55 +248,55 @@ m(ComponentUsingThis, {text: "Hello"})
|
247 | 248 | // <div>Hello</div>
|
248 | 249 | ```
|
249 | 250 |
|
250 |
| -Be aware that when using ES5 functions, the value of `this` in nested anonymous functions is not the component instance. There are two recommended ways to get around this JavaScript limitation, use ES6 arrow functions, or if ES6 is not available, use `vnode.state`. |
| 251 | +Be aware that when using ES5 functions, the value of `this` in nested anonymous functions is not the component instance. There are two recommended ways to get around this JavaScript limitation, use arrow functions, or if those are not supported, use `vnode.state`. |
251 | 252 |
|
252 | 253 | ---
|
253 | 254 |
|
254 |
| -### ES6 classes |
| 255 | +### Classes |
255 | 256 |
|
256 |
| -If it suits your needs (like in object-oriented projects), components can also be written using ES6 class syntax: |
| 257 | +If it suits your needs (like in object-oriented projects), components can also be written using classes: |
257 | 258 |
|
258 | 259 | ```javascript
|
259 |
| -class ES6ClassComponent { |
| 260 | +class ClassComponent { |
260 | 261 | constructor(vnode) {
|
261 |
| - this.kind = "ES6 class" |
| 262 | + this.kind = "class component" |
262 | 263 | }
|
263 | 264 | view() {
|
264 |
| - return m("div", `Hello from an ${this.kind}`) |
| 265 | + return m("div", `Hello from a ${this.kind}`) |
265 | 266 | }
|
266 | 267 | oncreate() {
|
267 |
| - console.log(`A ${this.kind} component was created`) |
| 268 | + console.log(`A ${this.kind} was created`) |
268 | 269 | }
|
269 | 270 | }
|
270 | 271 | ```
|
271 | 272 |
|
272 |
| -Component classes must define a `view()` method, detected via `.prototype.view`, to get the tree to render. |
| 273 | +Class components must define a `view()` method, detected via `.prototype.view`, to get the tree to render. |
273 | 274 |
|
274 | 275 | They can be consumed in the same way regular components can.
|
275 | 276 |
|
276 | 277 | ```javascript
|
277 | 278 | // EXAMPLE: via m.render
|
278 |
| -m.render(document.body, m(ES6ClassComponent)) |
| 279 | +m.render(document.body, m(ClassComponent)) |
279 | 280 |
|
280 | 281 | // EXAMPLE: via m.mount
|
281 |
| -m.mount(document.body, ES6ClassComponent) |
| 282 | +m.mount(document.body, ClassComponent) |
282 | 283 |
|
283 | 284 | // EXAMPLE: via m.route
|
284 | 285 | m.route(document.body, "/", {
|
285 |
| - "/": ES6ClassComponent |
| 286 | + "/": ClassComponent |
286 | 287 | })
|
287 | 288 |
|
288 | 289 | // EXAMPLE: component composition
|
289 |
| -class AnotherES6ClassComponent { |
| 290 | +class AnotherClassComponent { |
290 | 291 | view() {
|
291 | 292 | return m("main", [
|
292 |
| - m(ES6ClassComponent) |
| 293 | + m(ClassComponent) |
293 | 294 | ])
|
294 | 295 | }
|
295 | 296 | }
|
296 | 297 | ```
|
297 | 298 |
|
298 |
| -#### Class Component State |
| 299 | +#### Class component state |
299 | 300 |
|
300 | 301 | With classes, state can be managed by class instance properties and methods, and accessed via `this`:
|
301 | 302 |
|
@@ -324,14 +325,23 @@ class ComponentWithState {
|
324 | 325 | }
|
325 | 326 | ```
|
326 | 327 |
|
327 |
| -Note that we must wrap the event callbacks in arrow functions so that the `this` context is preserved correctly. |
| 328 | +Note that we must use arrow functions for the event handler callbacks so the `this` context can be referenced correctly. |
328 | 329 |
|
329 | 330 | ---
|
330 | 331 |
|
331 | 332 | ### Mixing component kinds
|
332 | 333 |
|
333 | 334 | Components can be freely mixed. A class component can have closure or POJO components as children, etc...
|
334 | 335 |
|
| 336 | +--- |
| 337 | + |
| 338 | +### Special attributes |
| 339 | + |
| 340 | +Mithril places special semantics on several property keys, so you should normally avoid using them in normal component attributes. |
| 341 | + |
| 342 | +- [Lifecycle methods](lifecycle-methods.md): `oninit`, `oncreate`, `onbeforeupdate`, `onupdate`, `onbeforeremove`, and `onremove` |
| 343 | +- `key`, which is used to track identity in keyed fragments |
| 344 | +- `tag`, which is used to tell vnodes apart from normal attributes objects and other things that are non-vnode objects. |
335 | 345 |
|
336 | 346 | ---
|
337 | 347 |
|
|
2 commit comments
soulofmischief commentedon Sep 6, 2019
@isiahmeadows were those references to mocha in lines 11 & 19 of
docs/testing.md
accidental?dead-claudia commentedon Sep 6, 2019
@soulofmischief Yes. Feel free to file a pull request if you'd like.