Skip to content

Commit be30ab8

Browse files
committed
move counter sample to react chapter
1 parent 80dacff commit be30ab8

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

03-bundling/06-vite/07-react/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,41 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
147147
⚡ 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.
148148

149149
⚡ 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.
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
import { FC } from "react";
1+
import { FC, useEffect, useState } from "react";
22

33
export const HelloComponent: FC = () => {
4-
return <h2>Hello from React</h2>;
4+
const [counter, setCounter] = useState(0);
5+
6+
useEffect(() => {
7+
const timer = setInterval(() => {
8+
setCounter(prev => prev + 1);
9+
}, 1_000);
10+
11+
return () => clearInterval(timer);
12+
}, []);
13+
14+
return (
15+
<>
16+
<h2>Hello from React</h2>
17+
<p>Counter state: {counter}</p>
18+
</>
19+
);
520
};

03-bundling/06-vite/10-code-splitting/README.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,6 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
3232

3333
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.
3434

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-
6735
Now we can go the topic: **code splitting**. In this example we're going to use a module that is loaded on demand.
6836

6937
- Let's add _src/math.ts_ file.

0 commit comments

Comments
 (0)