UNPKG

3.23 kBMarkdownView Raw
1Composi
2=======
3
4Contents
5--------
6- [Components](./components.md)
7- [JSX](./jsx.md)
8- [Hyperx](./hyperx.md)
9- Hyperscript
10- [Mount and Render](./render.md)
11- [State](./state.md)
12- [Lifecycle Methods](./lifecycle.md)
13- [Events](./events.md)
14- [Styles](./styles.md)
15- [Unmount](./unmount.md)
16- [Installation](../README.md)
17- [Third Party Libraries](./third-party.md)
18- [Functional Components](./functional-components.md)
19- [Deployment](./deployment.md)
20
21hyperscript - h
22---------------
23
24Although most people are fine using JSX, some people hate it and would rather use anything else. Hyperscript is an alternate way of defining markup. In fact, when you build your project, Babel converts all JSX into hyperscript. Composi provides the `h` function to enable hyperscript compatible usage.
25
26`h` expects three arguments:
27
281. tag name
292. propertes/attributes
303. children
31
32Tag Name
33--------
34
35The first argument is a tag name. This is a lowercase HTML tag.
36```javascript
37const h1 = h('h1')
38const div = h('div')
39const p = h('p')
40const ul = h('ul')
41````
42
43Properties/Attribues
44--------------------
45
46Often you want to be able to give an element a property or attribute. We're talking about `class`, `id`, `checked`, `disabled`, etc. You can add properties and attributes by providing a second argument. This will be a key/value pair wrapped in curly braces:
47
48```javascript
49const h1 = h('h1', {class: 'title'})
50const button = h('button', {disabled: true})
51```
52
53When an element has no properties, you can use empty curly braces or `null`:
54
55```javascript
56const h1 = h('h1', {})
57const button = h('button', null)
58```
59
60Children
61--------
62
63The final argument allows you to define children for an element. There are two kinds of children: a text node, or other HTML elements. Providing text as an element's child is easy, just use a quoted string:
64
65```javascript
66const h1 = h('h1', {class: 'title'}, 'This is the Title!')
67```
68
69To indicate that an element has child elements, use brackets with hyperscript defining those elements. Examine these two examples carefully:
70
71```javascript
72const header = h('header', {}, [
73 h('h1', {}, 'This is the Title!'),
74 h('h2', {}, 'This is the Subtitle!')
75])
76
77const list = h('ul', {class: 'list'}, [
78 h('li', {}, 'Apples'),
79 h('li', {}, 'Oranges'),
80 h('li', {}, 'Bananas')
81])
82```
83
84If you need to create an element's children dynamically based on a set of data, you can use JavaScript for that. Notice how we use a template literal to output the object values:
85
86```javascript
87const fruits = {
88 {
89 name: 'Apple',
90 price: '.50'
91 },
92 {
93 name: 'Orange',
94 price: '.75'
95 },
96 {
97 name: 'Banana',
98 price: '.30'
99 }
100}
101
102// ES5 version:
103const list = h('ul', {class: 'list'}, [
104 fruits.map(function(fruit) {
105 return h('li', {}, `${fruit.name}: $${fruit.price}`)
106 })
107])
108
109// ES6 version:
110const list = h('ul', {class: 'list'}, [
111 fruits.map(fruit => h('li', {}, `${fruit.name}: $${fruit.price}`))
112])
113```
114
115Summary
116-------
117
118The hyperscript function `h` lets you define markup with JavaScript functions. If you do not like the look and feel of JSX, this is a good alternative. This `h` function is similar to [React.createElement](https://facebook.github.io/react/docs/react-api.html#createelement)
\No newline at end of file