Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit fbe1468

Browse files
committed
New and clearer descriptors proposal
1 parent b07370c commit fbe1468

6 files changed

+125
-76
lines changed

Diff for: 01 - Core/01 - Classes.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Instead of using @jsx directive we import the HTML export from react-dom
2-
// and bind it to the JSX identifier. This models the DOM dependency explicitly.
3-
// The JSX transpiler uses this identifier to resolve HTML tags. E.g. JSX.div();
4-
import { HTML as JSX } from "react-dom";
5-
61
// The only reason we need a React depencency here is because the base class
72
// provides the this.setState method.
83
import { Component } from "react";

Diff for: 01 - Core/02 - Mixins.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Component } from "react";
2-
import { HTML as JSX } from "react-dom";
31
import { mixin } from "react-utils";
42

53
// Chainable mixins
@@ -44,9 +42,12 @@ class C extends mixin(A, B) {
4442
C.getQueries(); // B, C
4543
new C().componentDidMount(); // B, A, C
4644

45+
46+
import { Component } from "react";
47+
4748
// A component that mixes in all of C's functionality
4849

49-
class Component extends mixin(React.Component, C) {
50+
class Component extends mixin(Component, C) {
5051
render() {
5152
return <div />;
5253
}

Diff for: 01 - Core/03 - Stateless Functions.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Instead of using @jsx directive we import the HTML export from react-dom
2-
// and bind it to the JSX identifier.
3-
import { HTML as JSX } from "react-dom";
4-
51
// A simple component, that isn't stateful, can be provided as a single
62
// function that accepts props. This provides React with a hint that this
73
// component can be collapsed and that it's state doesn't need to be preserved.

Diff for: 01 - Core/05 - Descriptor Factories.js

-62
This file was deleted.

Diff for: 01 - Core/05 - Descriptors.js

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* DESCRIPTOR OBJECT LITERAL
3+
*
4+
* A React component module will no longer export a helper function to create
5+
* descriptors. Instead it's the responsibility of the consumer to efficiently
6+
* create the descriptors.
7+
*
8+
* Languages that compile to JS can choose to implement the descriptor signature
9+
* in whatever way is idiomatic for that language:
10+
*/
11+
12+
{
13+
type: Button,
14+
props: {
15+
...Button.defaultProps, // optional
16+
foo: 'bar',
17+
children: [
18+
{ type: 'span', props: { children: a } },
19+
{ type: 'span', props: { children: b } }
20+
]
21+
},
22+
23+
// optional
24+
key: 'mybutton',
25+
ref: myButtonRef,
26+
27+
// optional
28+
owner: React.currentOwner,
29+
context: React.currentContext
30+
}
31+
32+
/**
33+
* JSX
34+
*/
35+
36+
<Button foo="bar" key="mybutton" ref={myButtonRef}>
37+
<span>{a}</span>
38+
<span>{b}</span>
39+
</Button>
40+
41+
/**
42+
* PLAIN JS
43+
*
44+
* This helper function ensures that your static children don't get the key
45+
* warning. It creates a descriptor for you with the current owner/context.
46+
* The props object is cloned and key/ref moved onto the descriptor.
47+
*/
48+
49+
var C = React.createDescriptor;
50+
51+
C(Button, { foo: 'bar', key: 'mybutton', ref: myButtonRef },
52+
C('span', null, a),
53+
C('span', null, b)
54+
)
55+
56+
/**
57+
* If you use JSX or statically can analyze that the C(...) call belongs to
58+
* React, then you can chose to opt-in to one of the optimizations modes.
59+
*/
60+
61+
/**
62+
* FAST MODE
63+
*
64+
* Fast mode allocates the final props object inline instead of cloning a
65+
* temporary object. If defaultProps can be statically resolved, their value is
66+
* inlined (baz: 1) instead of dynamically resolved. This should not be typed
67+
* manually in user code.
68+
*/
69+
70+
var F = React.createDescriptorFast;
71+
72+
F(Button, 'mybutton', myButtonRef, { foo: 'bar', baz: 1, children: [
73+
F('span', null, null, a),
74+
F('span', null, null, b)
75+
]})
76+
77+
/**
78+
* POOLED MODE
79+
*
80+
* Pooled mode doesn't allocate any new objects. Instead it gets mutable objects
81+
* from an pool and reuses them. It overrides the props on the pooled object.
82+
*/
83+
84+
var P = React.getPooledDescriptor;
85+
var A = React.getPooledArray;
86+
var t1, t2;
87+
88+
(t1 = P(Button, 'mybutton', myButtonRef), t1.props.foo = 'bar', t1.props.children = A(
89+
(t2 = P('span'), t2.props.children = a, t2),
90+
(t2 = P('span'), t2.props.children = b, t2)
91+
), t1)
92+
93+
/**
94+
* NATIVE COMPONENTS
95+
*
96+
* Note that DOM nodes are no longer functions on React.DOM, instead they're
97+
* just strings. JSX will convert any lower-case tag name, or if it has a dash,
98+
* into a string value instead of a scope reference. This makes them compatible
99+
* with custom tags (Web Components).
100+
*
101+
* There's no direct dependency between a component and the React runtime.
102+
*
103+
* A tree of just native components should be JSON serializable.
104+
*/
105+
106+
/**
107+
* TEMPLATE STRINGS
108+
*
109+
* You could great an add-on sugar which uses ES6 template strings to create
110+
* descriptors. It becomes more palatable if all your components are registered
111+
* through strings.
112+
*/
113+
114+
X`
115+
<my-button foo="bar" key="mybutton" ref=${myButtonRef}>
116+
<span>${a}</span>
117+
<span>${b}</span>
118+
</my-button>
119+
`

Diff for: 01 - Core/08 - Transferring Props.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { HTML as JSX, joinClasses } from "react-dom";
1+
import { HTMLProps, joinClasses } from "react-dom";
22

33
// Draft
44

5-
interface FancyButtonProps extends typeof JSX.button.prototype.props {
5+
interface FancyButtonProps extends typeof HTMLProps.button {
66
color: string,
77
width: number,
88
height: number

0 commit comments

Comments
 (0)