This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy path05 - Elements.js
145 lines (126 loc) · 3.42 KB
/
05 - Elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* ELEMENT OBJECT LITERAL
*
* A React component module will no longer export a helper function to create
* virtual elements. Instead it's the responsibility of the consumer to
* efficiently create the virtual element.
*
* Languages that compile to JS can choose to implement the element signature
* in whatever way is idiomatic for that language:
*/
{
type: Button,
props: {
foo: bar,
children: [
{ type: 'span', props: { children: a } },
{ type: 'span', props: { children: b } }
]
},
// optional
key: 'mybutton',
ref: myButtonRef
}
/**
* JSX
*/
<Button foo={bar} key="mybutton" ref={myButtonRef}>
<span>{a}</span>
<span>{b}</span>
</Button>
/**
* PLAIN JS
* __DEV__ MODE
*
* This helper function ensures that your static children don't get the key
* warning. It creates an element for you with the current owner/context.
* The props object is cloned and key/ref moved onto the element.
*/
var _Button = React.createFactory(Button);
var _span = React.createFactory('span');
_Button({ foo: bar, key: 'mybutton', ref: myButtonRef },
_span(null, a),
_span(null, b)
)
/**
* If you use JSX, or can statically analyze that the Factory calls belongs to
* React, then you can chose to opt-in to one of the optimizations modes.
*/
/**
* INLINE
* PRODUCTION MODE
*
* Inline mode simply creates the element objects inline in the code, with
* a lookup for current owner/context as well as resolving default props.
* If defaults aren't known statically, then we create a factory that can help
* assign defaults quickly on the newly created object.
*/
var Button_assignDefaults = React.createDefaultsFactory(Button);
{
type: Button,
props: Button_assignDefaults({
foo: bar,
children: [
{ type: 'span', props: { children: a }, key: null, ref: null, _owner: React._currentOwner, _context: React._currentContext },
{ type: 'span', props: { children: b }, key: null, ref: null, _owner: React._currentOwner, _context: React._currentContext }
]
}),
key: 'mybutton',
ref: myButtonRef,
_owner: React._currentOwner,
_context: React._currentContext
}
/**
* POOLED MODE
*
* Pooled mode doesn't allocate any new objects. Instead it gets mutable objects
* from a pool and reuses them. It overrides the props on the pooled object.
*/
var P1 = React.createElementPool({
type: Button,
key: 'mybutton',
props: {
foo: null,
children: null
}
});
var P2 = React.createElementPool({
type: 'span',
props: {
children: null
}
});
var A2 = React.createArrayPool(2); // Number of items in the array
var t1, t1p, t1c, t2;
(
t1 = P1(),
t1.ref = myButtonRef,
t1p = t1.props,
t1p.foo = bar,
t1p.children = A2(),
t1c = t1p.children,
t1c[0] = (t2 = P2(), t2.props.children = a, t2),
t1c[1] = (t2 = P2(), t2.props.children = b, t2),
t1
)
/**
* NATIVE COMPONENTS
*
* Note that DOM nodes are no longer functions on React.DOM, instead they're
* just strings. JSX will convert any lower-case tag name, or if it has a dash,
* into a string value instead of a scope reference. This makes them compatible
* with custom tags (Web Components).
*/
/**
* TEMPLATE STRINGS
*
* You could create an add-on sugar which uses ES6 template strings to create
* elements. It becomes more palatable if all your components are registered
* through strings.
*/
X`
<my-button foo=${bar} key="mybutton" ref=${myButtonRef}>
<span>${a}</span>
<span>${b}</span>
</my-button>
`