UNPKG

3.46 kBMarkdownView Raw
1Composi
2=======
3
4Contents
5--------
6- [Installation](../README.md)
7- [JSX](./jsx.md)
8- [Hyperx](./hyperx.md)
9- Hyperscript
10 - [hyperscript -h](#hyperscript---h)
11 - [Tag Name](#Tag-Name)
12 - [Properties/Attributes](#Properties/Attributes)
13 - [Children](#Children)
14 - [Summary](#Summary)
15- [Functional Components](./functional-components.md)
16- [Mount, Render and Unmount](./render.md)
17- [Components](./components.md)
18- [State](./state.md)
19- [Lifecycle Methods](./lifecycle.md)
20- [Events](./events.md)
21- [Styles](./styles.md)
22- [Unmount](./unmount.md)
23- [State Management with DataStore](./data-store.md)
24- [Third Party Libraries](./third-party.md)
25- [Deployment](./deployment.md)
26- [Differrences with React](./composi-react.md)
27
28
29## hyperscript - h
30
31Although 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.
32
33`h` expects three arguments:
34
351. tag name
362. propertes/attributes
373. children
38
39## Tag Name
40
41The first argument is a tag name. This is a lowercase HTML tag.
42```javascript
43const h1 = h('h1')
44const div = h('div')
45const p = h('p')
46const ul = h('ul')
47````
48
49## Properties/Attributes
50
51Often 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:
52
53```javascript
54const h1 = h('h1', {class: 'title'})
55const button = h('button', {disabled: true})
56```
57
58When an element has no properties, you can use empty curly braces or `null`:
59
60```javascript
61const h1 = h('h1', {})
62const button = h('button', null)
63```
64
65## Children
66
67The 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:
68
69```javascript
70const h1 = h('h1', {class: 'title'}, 'This is the Title!')
71```
72
73To indicate that an element has child elements, use brackets with hyperscript defining those elements. Examine these two examples carefully:
74
75```javascript
76const header = h('header', {}, [
77 h('h1', {}, 'This is the Title!'),
78 h('h2', {}, 'This is the Subtitle!')
79])
80
81const list = h('ul', {class: 'list'}, [
82 h('li', {}, 'Apples'),
83 h('li', {}, 'Oranges'),
84 h('li', {}, 'Bananas')
85])
86```
87
88If 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:
89
90```javascript
91const fruits = {
92 {
93 name: 'Apple',
94 price: '.50'
95 },
96 {
97 name: 'Orange',
98 price: '.75'
99 },
100 {
101 name: 'Banana',
102 price: '.30'
103 }
104}
105
106// ES5 version:
107const list = h('ul', {class: 'list'}, [
108 fruits.map(function(fruit) {
109 return h('li', {}, `${fruit.name}: $${fruit.price}`)
110 })
111])
112
113// ES6 version:
114const list = h('ul', {class: 'list'}, [
115 fruits.map(fruit => h('li', {}, `${fruit.name}: $${fruit.price}`))
116])
117```
118
119## Summary
120
121The 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