Slide 1

Slide 1 text

Introduction to React Compiler @yossydev 1

Slide 2

Slide 2 text

Profile Name: ユウト Field: Web Frontend Developer X Bluesky Blog Youtube 2

Slide 3

Slide 3 text

Agenda 1. What is React 2. What is React Compiler 3. What makes the React Compiler happy? 4. React Compiler's HIR 5. Summary 3

Slide 4

Slide 4 text

What is React React とは? 4

Slide 5

Slide 5 text

What is React Libraries that make it easier for developers to create web apps. 訳: Web アプリを開発者が作りやすいようにしてくれるライブラリ other tool Vue Svelte Astro SoildJS Qwik HonoX 5

Slide 6

Slide 6 text

What is React Compiler React Compiler とは? 6

Slide 7

Slide 7 text

What is React Compiler React Compiler is a compiler that optimizes React applications, ensuring that only the minimal parts of components and hooks will re-render when state changes. The compiler also validates that components and hooks follow the Rules of React. https://github.com/facebook/react/blob/main/compiler/README.md React Compiler is a compiler that optimizes React applications 訳: React Compiler はReact アプリケーションを最適化するコンパイラ 7

Slide 8

Slide 8 text

What is React Compiler Foo is re-rendered every time a button is clicked. 訳: button をクリックするたびにFoo が再レンダリングされてしまう const App = () => { const [count, setCount] = useState(0); return ( <> setCount((count) => count + 1)}> count is {count} ); }; // Not memoised. const Foo = () => { console.log(" レンダリング!"); return <>; }; 8

Slide 9

Slide 9 text

What is React Compiler Developer memoing to prevent unnecessary re-rendering. 訳: 開発者がメモ化して不要な再レンダリングを防ぐ const App = () => { const [count, setCount] = useState(0); return ( <> setCount((count) => count + 1)}> count is {count} ); }; // Not memoised. const Foo = memo(() => { console.log(" レンダリング!"); return <>; }); 9

Slide 10

Slide 10 text

What is React Compiler React basically re-renders everything, so use the following API to optimise (memoise) 訳: React は基本的に全てを再レンダリングするので、以下のAPI を使って最適化(メモ 化)を行なう useMemo useCallback React.memo 10

Slide 11

Slide 11 text

What is React Compiler React Compiler does not use these and performs optimisations automatically! 訳: React Compiler はこれらを使用せず、自動的に最適化を行います! - useMemo - useCallback - React.memo 11

Slide 12

Slide 12 text

What makes the React Compiler happy? React Compiler は何が嬉しいの? 12

Slide 13

Slide 13 text

What makes the React Compiler happy? 1. Manual memoization → Auto memoization 2. Code can be checked by the compiler. 13

Slide 14

Slide 14 text

Manual memoization → Auto memoization 手動メモ化 → 自動メモ化 14

Slide 15

Slide 15 text

Manual memoization → Auto memoization Example) import { useState } from "react"; export default function MyApp() { const [name, setName] = useState(""); const [address, setAddress] = useState(""); const handleSubmit = (orderDetails) => { setAddress("122-2222"); post("/product/" + productId + "/buy", { referrer, orderDetails, }); }; j; const visibleTodos = () => filterTodos(todos, tab); return ( <>

{address}

Click!! Click!! ); } function Greeting({ name }) { console.log("Greeting was rendered at", new Date().toLocaleTimeString()); return (

Hello{name && ", "} {name}!

); } 15

Slide 16

Slide 16 text

Manual memoization → Auto memoization Example) part of one function MyApp() { const $ = _c(8); ...other const handleSubmit = t0; const visibleTodos = _temp; let t2; let t3; let t4; if ($[3] === Symbol.for("react.memo_cache_sentinel")) { t2 = ; t3 = Click!!; t4 = Click!!; $[3] = t2; $[4] = t3; $[5] = t4; } else { t2 = $[3]; t3 = $[4]; t4 = $[5]; } return t5; } ...other 16

Slide 17

Slide 17 text

Manual memoization → Auto memoization Example) part of one function MyApp() { const $ = _c(8); // Generate an Array with a capacity of 8 ...other const handleSubmit = t0; const visibleTodos = _temp; let t2; let t3; let t4; if ($[3] === Symbol.for("react.memo_cache_sentinel")) { // Cache if Symbol.for("react.memo_cache_sentinel") is true. t2 = ; t3 = Click!!; t4 = Click!!; $[3] = t2; $[4] = t3; $[5] = t4; } else { // If Symbol.for("react.memo_cache_sentinel") is false, return cached results. t2 = $[3]; t3 = $[4]; t4 = $[5]; } return t5; } ...other 17

Slide 18

Slide 18 text

Manual memoization → Auto memoization For me, it's good that this reduces communication about whether to memoise or not. 訳: メモ化するしないのコミュニケーションも減らせる a. 「Is memoing here necessary? 」 b. 「Necessary because 〇〇 is 〇〇」 18

Slide 19

Slide 19 text

Code can be checked by the compiler コンパイラによるコードのチェックができる 19

Slide 20

Slide 20 text

Code can be checked by the compiler Question) Do you think this code is an error? 訳: このコードはエラーになると思いますか? function Component(props) { const items = []; for (const x of props.items) { x.modified = true; items.push(x); } return items; } 20

Slide 21

Slide 21 text

code can be checked by the compiler Answer) Compilation error! InvalidReact: Mutating component props or hook arguments is not allowed. Consider using a local variable instead (4:4) function Component(props) { const items = []; for (const x of props.items) { x.modified = true; items.push(x); } return items; } 21

Slide 22

Slide 22 text

Code can be checked by the compiler 他にもエラーになるケースはたくさんあります!playground を触ったり、テストケー スを見たりしてみてください! 22

Slide 23

Slide 23 text

まとめ 23

Slide 24

Slide 24 text

まとめ React Compiler がReact19 から入る 自動で最適化を行ってくれたり、変なコードはコンパイルエラーを出してくれる ようになる 特段何か設定する必要はない babel プラグインとして使う フレームワーク側の設定に合わせる 他にも面白いものあるよ!(eslintSuppressionRules, opt-in/opt-out, HIR... ) 24

Slide 25

Slide 25 text

React19 楽しみです!!!!! 25