-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
415 lines (401 loc) · 14.4 KB
/
index.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link
rel="icon"
type="image/svg+xml"
href="https://i.pinimg.com/564x/be/6e/30/be6e30b5f785278644c4e4e019cde02d.jpg"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Compiler: Presentation</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/reveal.min.css" integrity="sha512-0AUO8B5ll9y1ERV/55xq3HeccBGnvAJQsVGitNac/iQCLyDTGLUBMPqlupIWp/rJg0hV3WWHusXchEIdqFAv1Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/theme/white.min.css" integrity="sha512-NJYOiL4HPnu7QvvGcvHAnVVz2X6qWcTDAH4x3fmUOW/SGUbK46IbzHhQ1KoKDfK2trs5RwUu03Ax9sxjqz1iQw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/plugin/highlight/zenburn.min.css" integrity="sha512-JPxjD2t82edI35nXydY/erE9jVPpqxEJ++6nYEoZEpX2TRsmp2FpZuQqZa+wBCen5U16QZOkMadGXHCfp+tUdg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="/react-compiler-presentation/public/presentation-styles.css" />
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<div class="side-by-side">
<img src="/react-compiler-presentation/public/react_uwu_logo.png" height="165" alt="react logo">
<h1>Compiler</h1>
</div>
<p>The near future of how we will use React</p>
</section>
<section data-markdown>
<textarea data-template>
## Let's Start with the Basics
We will create a simple address form app
</textarea>
</section>
<section>
<section>
<h2>What we have (1)</h2>
<p>A global context named FormContext</p>
<pre>
<code class="language-typescript">
const FormContext = createContext(defaultFormContext);
const useForm = () => useContext(FormContext);
function FormProvider({ children }) {
const [data, setData] = useState<FormFields>({
name: createDefaultInputState(),
// ...
});
const updateField = <K>(field: K, value: Partial<FormFields[K]>) => {
setData(prevData => ({
...prevData,
[field]: {
...prevData[field],
...value,
},
}));
};
return (
<FormContext.Provider
value={{
...data,
updateField,
}}
>
{children}
</FormContext.Provider>
);
}
</code>
</pre>
</section>
<section>
<h2>What we have (2)</h2>
<p>Two controlled form components like this:</p>
<pre>
<code class="language-typescript" data-line-numbers="1-26|15-21">
const Select = ({
id, label, options, value, onChange,
}) => {
const handleChange = (event) => {
onChange(event.target.value);
};
return (
<div className="select-wrapper">
<label htmlFor="select" className="select-label">
{label}
</label>
<select id={id} value={value || ''} onChange={handleChange}>
{options.map((option) => {
increaseRenderCount('select-option');
return (
<option key={option.value} value={option.value}>
{option.label}
</option>
);
})}
</select>
</div>
);
};
</code>
</pre>
</section>
<section>
<h2>What we have (3)</h2>
<p>A form component</p>
<pre>
<code class="language-typescript" data-line-numbers="1-26|5-6|10-15|17-21">
const Form = () => {
const { name, surname, city, district, address, updateField } = useForm();
// We will keep track of this fn calls just in case:
const filteredDistrictList = getDistrictList(city.value);
return (
<div>
<Input
value={name.value}
onChange={(event) =>
updateField('name', { value: event.currentTarget.value })
}
/>
<Input id="surname" {/* ... */} />
<Select
options={CITY_LIST}
value={city.value}
onChange={(value) => updateField('city', { value: Number(value) })}
/>
<Select options={filteredDistrictList} {/* ... */} />
<Input id="address" {/* ... */} />
</div>
);
}
</code>
</pre>
</section>
</section>
<section>
<div class="side-by-side text-align-start">
<iframe data-src="/react-compiler-presentation/non-compiled" data-preload width="700" height="500"></iframe>
<div class="fragment margin-inline-start" style="max-width: 300px;">
<h5>Issues</h5>
<ul class="small-list">
<li>1. State change causes most elements to re-render.</li>
<li>2. Frequently re-rendering select component is expensive.</li>
<li>3. Frequently calling district filter function is expensive.</li>
</ul>
<img src="/react-compiler-presentation/public/react-rerender-meme.JPG">
</div>
</div>
</section>
<section data-markdown>
<textarea data-template>
## There are lots of solutions
But what if we could solve all these issues effortlessly?
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## React Compiler
A build-time tool that automatically optimizes your React app.
---
## Prerequisites
- Following the [Rules of React](https://react.dev/reference/rules).
- Best optimized for React 19 RC.
- Compatible with React 17+ with [additional configuration](https://react.dev/learn/react-compiler#using-react-compiler-with-react-17-or-18).
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Using the React Compiler with ESLint Plugin
- React Compiler powers an ESLint plugin.
- It displays any violations of React rules in your editor.
- It can be used *independently*.
</textarea>
</section>
<section>
<p class="small-text">When the rule does not pass, it means that the compiler has skipped over optimizing that component or hook.</p>
<pre>
<code class="language-typescript" data-line-numbers="2|7,10">
import reactCompiler from 'eslint-plugin-react-compiler'
export default [
{
plugins: {
'react-compiler': reactCompiler,
},
rules: {
'react-compiler/react-compiler': 'warn',
},
},
];
</code>
</pre>
</section>
<section>
<p>Example compiler lint issue:</p>
<img class="r-stretch" src="/react-compiler-presentation/public/react-compiler-eslint-example.png" alt="React Compiler Eslint Example">
</section>
<section>
<strong>Running healthcheck script</strong>
<img src="/react-compiler-presentation/public/react-compiler-healthcheck-example.png" width="700" alt="React Compiler Healthcheck Example">
</section>
<section>
<p>We will integrate the React Compiler with Babel.</p>
<small>The React Compiler supports most build tools.</small>
<pre>
<code class="language-typescript" data-line-numbers="11-21|2-5|22-35">
import {
defineReactCompilerLoaderOption,
reactCompilerLoader,
} from "react-compiler-webpack";
module.exports = {
// ...
module: {
rules: [
// Current:
{
test: /\.(js|jsx)$/,
exclude: /node_modules\/.*/,
use: {
loader: "babel-loader",
options: {
presets: [["@babel/preset-env", { targets: "defaults" }]],
},
},
},
// New:
{
test: /\.[mc]?[jt]sx$/i,
exclude: /node_modules/,
use: [
{
loader: reactCompilerLoader,
options: defineReactCompilerLoaderOption({
// React Compiler options goes here
// compilationMode: 'annotation',
}),
},
],
},
],
},
} satisfies WebpackConfiguration;
</code>
</pre>
</section>
<section>
<h1>That's it ? 🤔</h1>
</section>
<section>
<iframe data-src="/react-compiler-presentation/compiled" data-preload width="700" height="500"></iframe>
</section>
<section>
<h3>🤯 What did the compiler do?</h3>
<img src="/react-compiler-presentation/public/react-dev-tools-change-example.png" alt="React Dev Tools Change Example">
</section>
<section>
<h3>Let's Go Back to Our Select Component</h3>
<small>More simplified for easier tracking</small>
<pre>
<code class="language-typescript" data-line-numbers="1-18|11-15">
export const Select = ({
id, options, value, onChange,
}: SelectProps) => {
return (
<select
id={id}
value={value || ""}
onChange={onChange}
>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
);
};
</code>
</pre>
</section>
<section>
<h3>Standard Webpack Output</h3>
<small>Without compiler integration</small>
<pre>
<code class="language-typescript" data-line-numbers="2|4-20">
import { jsx as _jsx } from "react/jsx-runtime";
export const Select = ({ id, options, value, onChange, }) => {
return (
_jsx("select", {
id: id,
value: value || "",
onChange: onChange,
children: options.map(
(option) => (
_jsx("option", {
value: option.value,
children: option.label
}, option.value)
)
)
})
);
};
</code>
</pre>
</section>
<section>
<h3>Webpack + React Compiler Output</h3>
<pre>
<code class="language-typescript" data-line-numbers="33-36|3|5-6|10-18|19-29|30">
import { jsx as _jsx } from "react/jsx-runtime";
import { c as _c } from "react/compiler-runtime";
export const Select = t0 => {
const $ = _c(7);
const { id, options, value, onChange } = t0;
const t1 = value || "";
// Auto generated memoization (low level)
let t2;
if ($[0] !== options) {
t2 = options.map(_temp);
$[0] = options;
$[1] = t2;
} else {
t2 = $[1];
}
let t3;
if ($[2] !== id || $[3] !== onChange || $[4] !== t1 || $[5] !== t2) {
t3 = _jsx("select", { id: id, value: t1, onChange: onChange, children: t2 });
$[2] = id;
$[3] = onChange;
$[4] = t1;
$[5] = t2;
$[6] = t3;
} else {
t3 = $[6];
}
return t3;
};
// Function Outlining
function _temp(option) {
return _jsx("option", { value: option.value, children: option.label }, option.value);
}
</code>
</pre>
</section>
<section>
<h4>Moving Progressive</h4>
<p class="small-text">Directory/path based filter:</p>
<pre>
<code class="language-typescript">
{
loader: reactCompilerLoader,
options: defineReactCompilerLoaderOption({
sources: (filename) => {
return filename.indexOf('src/path/to/dir') !== -1;
},
}),
}
</code>
</pre>
<p class="small-text"><code class="small-text">"use no memo";</code> directive:</p>
<pre>
<code class="language-typescript">
function SuspiciousComponent() {
"use no memo"; // opts out this component from being compiled by React Compiler
// ...
}
</code>
</pre>
</section>
<section>
<h4>Whats Next?</h4>
<ul>
<li>✅ Experimental Release <i>(@experimental)</i></li>
<li>✅ Public Beta Release <i>(@beta)</i></li>
<li>RC Release</li>
<li>General Availability</li>
</ul>
</section>
<section>
<h3>Source List</h3>
<ul>
<li><a href="https://react.dev/learn/react-compiler">React Docs - React Compiler</a></li>
<li><a href="https://playground.react.dev">React Compiler Playground</a></li>
<li><a href="https://www.youtube.com/watch?v=lGEMwh32soc">Youtube - React without memo | Xuan Huang (黄玄)</a></li>
<li><a href="https://www.youtube.com/watch?v=uA_PVyZP7AI">Youtube - React Compiler Deep Dive | Mofei Zhang & Sathya Gunasekaran</a></li>
<li><a href="https://www.youtube.com/watch?v=qd5yk2gxbtg">Youtube - What's next for the React Compiler? | Sathya Gunasekaran</a></li>
</ul>
</section>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/reveal.min.js" integrity="sha512-sMRSj1Ns64C2OE6VNS7WrV63OHW7dLAvi96CXRoy9AEe/tKF+868fhUJpc5ZKS166lwhe2ArCYjFitLJUY+VWA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/plugin/markdown/markdown.min.js" integrity="sha512-4exkEeyVuaWUFKozXl6L3UCugl6ai1cKnrVFkWUstdrNB2sDxxmPEaHBzTlYm9wX78EjPzEBG0s8k37oPeUFIw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.1.0/plugin/highlight/highlight.min.js" integrity="sha512-xkVKkN0o7xECTHSUZ9zdsBYRXiAKH7CZ3aICpW6aQJZsufVVRLhEBTDjTpC1tPzm+gNZiOeW174zXAB2fOLsTg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="module">
const deck = new Reveal({
plugins: [RevealMarkdown, RevealHighlight],
slideNumber: true,
fragmentInURL: true,
history: true,
});
deck.initialize();
</script>
</body>
</html>