Skip to content

Commit 20f0759

Browse files
authoredJul 23, 2019
Fix docs (#2482)
* Fix #2414, address part of #1687 Also cleared the CSS up to be a lot more readable instead of smooshed into a single line. * Redo the testing docs page - Addresses another part of #1687 - Also, fix a few linter issues in the ospec binary * Add note about third-party cookies, tweak a line * Make the JSX comparison much more meaningful And let the code speak for itself. Don't fuel the flame wars any more than what they've already become. We should be *unopinionated*, and so I've updated those docs to remove the existing opinion. * Remove a bunch of outdated ES6 references * Remove the CSS page
1 parent 61b087e commit 20f0759

11 files changed

+528
-286
lines changed
 

‎docs/components.md

+28-18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
- [State](#state)
77
- [Closure component state](#closure-component-state)
88
- [POJO component state](#pojo-component-state)
9-
- [ES6 Classes](#es6-classes)
9+
- [Classes](#classes)
1010
- [Class component state](#class-component-state)
11+
- [Special attributes](#special-attributes)
1112
- [Avoid anti-patterns](#avoid-anti-patterns)
1213

1314
### Structure
@@ -115,7 +116,7 @@ Note that unlike many other frameworks, mutating component state does *not* trig
115116

116117
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.
117118

118-
#### Closure Component State
119+
#### Closure component state
119120

120121
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.
121122

@@ -183,7 +184,7 @@ A big advantage of closure components is that we don't need to worry about bindi
183184

184185
---
185186

186-
#### POJO Component State
187+
#### POJO component state
187188

188189
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.
189190

@@ -247,55 +248,55 @@ m(ComponentUsingThis, {text: "Hello"})
247248
// <div>Hello</div>
248249
```
249250

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`.
251252

252253
---
253254

254-
### ES6 classes
255+
### Classes
255256

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:
257258

258259
```javascript
259-
class ES6ClassComponent {
260+
class ClassComponent {
260261
constructor(vnode) {
261-
this.kind = "ES6 class"
262+
this.kind = "class component"
262263
}
263264
view() {
264-
return m("div", `Hello from an ${this.kind}`)
265+
return m("div", `Hello from a ${this.kind}`)
265266
}
266267
oncreate() {
267-
console.log(`A ${this.kind} component was created`)
268+
console.log(`A ${this.kind} was created`)
268269
}
269270
}
270271
```
271272

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.
273274

274275
They can be consumed in the same way regular components can.
275276

276277
```javascript
277278
// EXAMPLE: via m.render
278-
m.render(document.body, m(ES6ClassComponent))
279+
m.render(document.body, m(ClassComponent))
279280

280281
// EXAMPLE: via m.mount
281-
m.mount(document.body, ES6ClassComponent)
282+
m.mount(document.body, ClassComponent)
282283

283284
// EXAMPLE: via m.route
284285
m.route(document.body, "/", {
285-
"/": ES6ClassComponent
286+
"/": ClassComponent
286287
})
287288

288289
// EXAMPLE: component composition
289-
class AnotherES6ClassComponent {
290+
class AnotherClassComponent {
290291
view() {
291292
return m("main", [
292-
m(ES6ClassComponent)
293+
m(ClassComponent)
293294
])
294295
}
295296
}
296297
```
297298

298-
#### Class Component State
299+
#### Class component state
299300

300301
With classes, state can be managed by class instance properties and methods, and accessed via `this`:
301302

@@ -324,14 +325,23 @@ class ComponentWithState {
324325
}
325326
```
326327

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.
328329

329330
---
330331

331332
### Mixing component kinds
332333

333334
Components can be freely mixed. A class component can have closure or POJO components as children, etc...
334335

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.
335345

336346
---
337347

‎docs/css.md

-94
This file was deleted.

2 commit comments

Comments
 (2)

soulofmischief commented on Sep 6, 2019

@soulofmischief
Contributor

@isiahmeadows were those references to mocha in lines 11 & 19 of docs/testing.md accidental?

dead-claudia commented on Sep 6, 2019

@dead-claudia
MemberAuthor

@soulofmischief Yes. Feel free to file a pull request if you'd like.

Please sign in to comment.