Skip to content

Commit e49859a

Browse files
committed
[React]: enhance useTransition() example
1 parent 358c587 commit e49859a

4 files changed

Lines changed: 68 additions & 89 deletions

File tree

react/useTransition/package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react/useTransition/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13-
"react": "^19.0.0-rc-cc1ec60d0d-20240607",
14-
"react-dom": "^19.0.0-rc-cc1ec60d0d-20240607",
15-
"sensorario-design-system": "^1.0.11"
13+
"react": "^19.0.0-rc-c21bcd627b-20240624",
14+
"react-dom": "^19.0.0-rc-c21bcd627b-20240624",
15+
"sensorario-design-system": "github:sensorario/sensorario-design-system#main"
1616
},
1717
"devDependencies": {
1818
"@types/react": "^18.2.66",

react/useTransition/src/App.tsx

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,63 @@
11
import { useState, useTransition } from "react";
22
import "sensorario-design-system/style/index.css";
33

4-
function Container() {
5-
const [page, setPage] = useState("/");
6-
const [isPending, startTransition] = useTransition();
7-
8-
function navigate(page: string) {
9-
startTransition(() => {
10-
setPage(page);
11-
});
12-
}
13-
14-
const renderMenu = () => {
15-
return (
16-
<div className="menu">
17-
<button onClick={() => navigate("/")}>home</button>
18-
<button onClick={() => navigate("/blog")}>blog</button>
19-
</div>
20-
);
21-
};
22-
23-
const renderLoading = () => {
24-
return (
25-
<>
26-
{renderMenu()}
27-
<div className="loading">loading ...</div>
28-
</>
29-
);
30-
};
31-
32-
const renderPage = (page: string) => {
33-
if (page == "/") return <HomePage />;
34-
if (page == "/blog") return <BlogPage />;
35-
};
36-
37-
if (isPending) return renderLoading();
38-
39-
return (
40-
<>
41-
{renderMenu()}
42-
{renderPage(page)}
43-
</>
44-
);
45-
}
46-
47-
function App() {
48-
return (
49-
<div className="sensorario-container">
50-
<h1>useTransition()</h1>
51-
<Container />
52-
</div>
53-
);
54-
}
55-
564
function delay(sec: number) {
575
let startTime = performance.now();
586
while (performance.now() - startTime < sec * 1000) {}
597
}
608

61-
function HomePage() {
9+
const Home = () => {
6210
delay(0.6);
6311

6412
return (
65-
<div className="sensorario-container">
66-
<h2>Homepage</h2>
13+
<div className="componente-home">
14+
<h1>Homepage</h1>
6715
</div>
6816
);
69-
}
17+
};
7018

71-
function BlogPage() {
72-
delay(0.5);
19+
const Blog = () => {
20+
delay(1);
7321

7422
return (
75-
<div className="sensorario-container">
76-
<h2>Blog!</h2>
23+
<div className="componente-home">
24+
<h1>Il mio blog</h1>
25+
<ul>
26+
<li>Post #1</li>
27+
<li>Post #2</li>
28+
<li>Post #3</li>
29+
</ul>
30+
</div>
31+
);
32+
};
33+
34+
function App() {
35+
const [page, setPage] = useState("/");
36+
const [isPending, startTransition] = useTransition();
37+
38+
function navigate(page: string) {
39+
startTransition(() => {
40+
setPage(page);
41+
});
42+
}
43+
44+
return (
45+
<div className="sensorario-container light">
46+
<h1>useTransition()</h1>
47+
<div className="button-menu">
48+
<div className="pagina-corrente">pagina corrente: {page}</div>
49+
<button disabled={isPending} onClick={() => navigate("/")}>
50+
home
51+
</button>
52+
<button disabled={isPending} onClick={() => navigate("/blog")}>
53+
blog
54+
</button>
55+
<div className="pagina">
56+
{isPending && "loading ..."}
57+
{page === "/" && <Home />}
58+
{page === "/blog" && <Blog />}
59+
</div>
60+
</div>
7761
</div>
7862
);
7963
}

react/useTransition/src/main.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom/client'
3-
import App from './App.tsx'
4-
import './index.css'
1+
import ReactDOM from "react-dom/client";
2+
import App from "./App.tsx";
3+
import "sensorario-design-system/style/index.css";
54

6-
ReactDOM.createRoot(document.getElementById('root')!).render(
7-
<React.StrictMode>
8-
<App />
9-
</React.StrictMode>,
10-
)
5+
ReactDOM.createRoot(document.getElementById("root")!).render(<App />);

0 commit comments

Comments
 (0)