You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03-bundling/06-vite/07-react/README.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -147,3 +147,41 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
147
147
⚡ Remember that dependencies, which are not likely to change, are pre-bundled in development flow using `esbuild`. This process is useful to harmonize modules format and optimize the number of requests needed to consume these dependencies.
148
148
149
149
⚡ With this approach, development gets amazingly light and fast. All of your heavy dependencies are consumed from your browser cache (except an occasional update) and your source code ES modules are ready in record time, just available for your browser to request them when needed.
150
+
151
+
152
+
## Optional - HMR
153
+
154
+
- ℹ️ `@vitejs/plugin-react` package also gives support for Fast Refresh, which is the specific HMR system for React. It properly communicates with React internal API (it is not public) to integrate HMR with Vite in an effective and efficient way, allowing to change code live while dev server is running without hard reloads
155
+
156
+
- ℹ️ It means we can change our app source code live, with the dev server running, and changes will be propagated to our browser without having to reload the page nor loosing the state.
157
+
158
+
- ⚡ In order to demonstrate this, let's implement a simple counter feature that gets updated automatically every second. This can be done with the following code:
159
+
160
+
_src/hello.tsx_
161
+
162
+
```diff
163
+
- import { FC } from "react";
164
+
+ import { FC, useEffect, useState } from "react";
165
+
166
+
export const HelloComponent: FC = () => {
167
+
+ const [counter, setCounter] = useState(0);
168
+
169
+
+ useEffect(() => {
170
+
+ const timer = setInterval(() => {
171
+
+ setCounter(prev => prev + 1);
172
+
+ }, 1_000);
173
+
174
+
+ return () => clearInterval(timer);
175
+
+ }, []);
176
+
177
+
return (
178
+
<>
179
+
<h2>Hello from React</h2>
180
+
+ <p>Counter state: {counter}</p>
181
+
<a
182
+
183
+
```
184
+
185
+
- 🔎 Now see the difference:
186
+
- If you reload the page manually, counter gets reset to 0 again.
187
+
- If you change something in your source code, app gets updated but counter does not loose its count.
Copy file name to clipboardExpand all lines: 03-bundling/06-vite/10-code-splitting/README.md
-32Lines changed: 0 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,38 +32,6 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
32
32
33
33
This means parts of the code are only loaded when needed (e.g. when a user navigates to a specific page or triggers a certain action), which reduces the initial bundle size, improves load times, and enhances user experience.
34
34
35
-
- ⚡ As a pre-step to prepare our sample, let's extend our app functionality with a simple counter feature that gets updated automatically every second. This can be done with the following code:
36
-
37
-
_src/hello.tsx_
38
-
39
-
```diff
40
-
- import { FC } from "react";
41
-
+ import { FC, useEffect, useState } from "react";
42
-
import config from "./env-config";
43
-
44
-
export const HelloComponent: FC = () => {
45
-
+ const [counter, setCounter] = useState(0);
46
-
47
-
+ useEffect(() => {
48
-
+ const timer = setInterval(() => {
49
-
+ setCounter(prev => prev + 1);
50
-
+ }, 1_000);
51
-
52
-
+ return () => clearInterval(timer);
53
-
+ }, []);
54
-
55
-
return (
56
-
<>
57
-
<h2>Hello from React</h2>
58
-
<p>Api server is {config.API_BASE}</p>
59
-
<p>Feature A is {config.IS_FEATURE_A_ENABLED ? "enabled" : "disabled"}</p>
60
-
+ <p>Counter state: {counter}</p>
61
-
<a
62
-
63
-
```
64
-
65
-
🔎 You can now check the browser to see the counter automatically getting increased.
66
-
67
35
Now we can go the topic: **code splitting**. In this example we're going to use a module that is loaded on demand.
0 commit comments