Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ํ๋‹นํ๋‹น ๐Ÿธ] React 19 ์ •๋ฆฌ #48

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react/jsx-runtime',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
settings: { react: { version: '18.2' } },
plugins: ['react-refresh'],
rules: {
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# React + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "react-19-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.0.0-canary-fd0da3eef-20240404",
"react-dom": "^19.0.0-canary-fd0da3eef-20240404"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"vite": "^5.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import UseStatusForm from "./components/UseStatusForm";
import UseStateForm from "./components/UseStateForm";
import Optimistic from "./components/Optimistic/Optimistic";

function App() {
return (
<div>
<section>
<h1>use form status</h1>
<UseStatusForm />
</section>
<hr />
<section>
<h1>use form state</h1>
<UseStateForm />
</section>
<section>
<h1>use optimistic</h1>
<Optimistic />
</section>
</div>
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useOptimistic, useState } from "react";

const DEFAULT_MESSAGES = [{ text: "Hey, I am initail", sending: false, key: 1 }];

const Optimistic = () => {
const [messages, setMessages] = useState(DEFAULT_MESSAGES);
const [optimisticMessages, addOptimisticMessage] = useOptimistic(messages, (state, newMessage) => [
...state,
{ text: newMessage, sending: true },
]);

async function handleSendFormData(formData) {
const sentMessage = await fakeDelayAction(formData.get("message"));
setMessages((messages) => [...messages, { text: sentMessage }]);
}

const handleSubmit = async (userData) => {
addOptimisticMessage(userData.get("username"));

await handleSendFormData(userData);
};

return (
<>
{optimisticMessages.map((message, index) => (
<div key={index}>
{message.text}
{!!message.sending && <small> (Sending...)</small>}
</div>
))}
<form action={handleSubmit}>
<h3>OptimisticState Hook</h3>
<div>
<label>Username</label>
<input type="text" name="username" />
</div>
<button type="submit">Submit</button>
</form>
</>
);
};

export default Optimistic;

async function fakeDelayAction(message) {
await new Promise((res) => setTimeout(res, 3000));
return message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./Optimistic";
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useFormState } from "react-dom";

const UseStateForm = () => {
const [message, formAction] = useFormState(handleSubmit, null);

return (
<form action={formAction}>
<label>Name</label>
<input type="text" name="username" />
<button>Submit</button>
{message && <h3>{message.text}</h3>}
</form>
);
};

const handleSubmit = (prevState, queryData) => {
const name = queryData.get("username");
console.log(prevState);

if (name === "Chanyeong") {
return { success: true, text: "Welcome" };
}
return { success: false, text: "Error" };
};

export default UseStateForm;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./UseStateForm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useFormStatus } from "react-dom";

const Submit = () => {
const { pending } = useFormStatus();

return <button disabled={pending}>{pending ? "Submitting..." : "Submit"}</button>;
};

export default Submit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Submit from "./Submit";

const UseStatusForm = () => {
return (
<form action={handleSubmit}>
<Submit />
</form>
);
};

const handleSubmit = () => new Promise((res) => setTimeout(res, 3000));

export default UseStatusForm;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./UseStatusForm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})
49 changes: 49 additions & 0 deletions React19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# react 19 ์ •๋ฆฌ

## ์ฃผ ๋ณ€ํ™”

### concurrent rendering

- react18์ด synchronous rendering์— ์˜์กดํ–ˆ๋˜๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, react19๋Š” ๋™์‹œ์„ฑ์„ ํ™œ์šฉํ•ด, UI ์—…๋ฐ์ดํŠธ๋ฅผ ๋” ์ž‘์€ ๋น„๋™๊ธฐ ์ž‘์—…์œผ๋กœ ์„ธ๋ถ„ํ™”ํ•จ
- ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์—…๋ฐ์ดํŠธ๋ฅผ ๋จผ์ € ์ฒ˜๋ฆฌํ•ด ๋” ๋งค๋„๋Ÿฝ๊ณ  ์œ ์—ฐํ•œ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ์ œ๊ณตํ•จ

### suspense for data fetching reinvented

- ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ ์‚ฌ์šฉํ•˜๋Š” suspense์— ๋Œ€ํ•œ ์„ฑ๋Šฅ ๊ฐœ์„ 
- suspense๊ฐ€ concurrent rendering๊ณผ ์›ํ™œํ•˜๊ฒŒ ํ†ตํ•ฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ ์˜ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ž‘์—…๊ณผ ๋ Œ๋”๋ง์„ ๋™์‹œ์— ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Œ

### server side rendering ๊ฐœ์„ 

- streaming๊ณผ concurrent๊ฐ€ ๋ฐœ์ „ํ–ˆ๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•ด ๋” ๋น ๋ฅธ TTFB์™€ ํ–ฅ์ƒ๋œ SEO ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•จ

> TTFB: Time to First Byte๋กœ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•œ ์‹œ์ ๊ณผ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ฒซ ๋ฒˆ์งธ ์ •๋ณด byte๋ฅผ ์ˆ˜์‹ ํ•œ ์‹œ์  ์‚ฌ์ด์˜ ์‹œ๊ฐ„

### ์ ์ง„์  ๋„์ž… ๊ฐ€๋Šฅ

- ๊ธฐ์กด react ์ƒํƒœ๊ณ„๊ฐ€ ๋‹ค์–‘ํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ธ์ง€ํ•˜๊ณ , ๊ฐœ๋ฐœ์ž๊ฐ€ react ํ”„๋กœ์ ํŠธ๋ฅผ ์ ์ง„์ ์œผ๋กœ upgradeํ•  ์ˆ˜ ์žˆ์Œ

### performance optimization

- react19๋Š” runtime ํšจ์œจ์€ ๋†’์ด๊ณ  ๋ฒˆ๋“ค ์‚ฌ์ด์ฆˆ๋Š” ์ค„์ด๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•จ
- ์ตœ์ ํ™”๋œ ์กฐ์ • ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ถ€ํ„ฐ ์ง€์—ฐ ์ปดํฌ๋„ŒํŠธ ๋กœ๋”ฉ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€, ๋” ๋น ๋ฅธ ๋ Œ๋”๋ง๊ณผ ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅ
- ์ƒˆ๋กœ์šด scheduler architecture๋ฅผ ๋„์ž…ํ•ด, ๋ Œ๋”๋ง ์šฐ์„ ์ˆœ์œ„์™€ resourceํ™œ์šฉ๋„๋ฅผ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•˜๊ณ  ์„ฑ๋Šฅ ํŠœ๋‹์„ ๊ฐ•ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์›€

### debugging developer tool

- react19๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋Š” component life cycle, state management, performance bottleneck ๋“ฑ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ insight๋ฅผ ํ™•๋ณดํ•˜๊ณ , ์•ฑ์„ ๋ณด๋‹ค ํšจ๊ณผ์ ์œผ๋กœ ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ์Œ
- time-slicing profiler์™€ flame charts์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์ด์ „๋ณด๋‹ค ํ–ฅ์ƒ๋œ ์ •๋ฐ€๋„๋กœ ์„ฑ๋Šฅ ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ์ง„๋‹จํ•˜๊ณ  ํ•ด๊ฒฐํ•˜๋„๋ก ์ง€์›

#### time-slicing profiler

#### flame chart

### ํ•ต์‹ฌ ๊ธฐ๋Šฅ

#### react compiler (experimental)

- react๋ฅผ ๋” ์ž‘๊ณ  ์ตœ์ ํ™”๋œ javascript๋กœ compileํ•จ์œผ๋กœ์จ, TTFB ๋ฐ UX ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ
- ์•„์ง experimental ๋‹จ๊ณ„๋กœ, ๋„๋ฆฌ ์‚ฌ์šฉํ•  ์ค€๋น„๋Š” ๋˜์–ด์žˆ์ง€ ์•Š์Œ

#### reference

- https://kmong.com/article/1852--React-19-%EC%B5%9C%EC%8B%A0-%EA%B8%B0%EB%8A%A5-%ED%95%9C%EB%88%88%EC%97%90-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0