|
1 | 1 | import { useState, useTransition } from "react"; |
2 | 2 | import "sensorario-design-system/style/index.css"; |
3 | 3 |
|
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 | | - |
56 | 4 | function delay(sec: number) { |
57 | 5 | let startTime = performance.now(); |
58 | 6 | while (performance.now() - startTime < sec * 1000) {} |
59 | 7 | } |
60 | 8 |
|
61 | | -function HomePage() { |
| 9 | +const Home = () => { |
62 | 10 | delay(0.6); |
63 | 11 |
|
64 | 12 | return ( |
65 | | - <div className="sensorario-container"> |
66 | | - <h2>Homepage</h2> |
| 13 | + <div className="componente-home"> |
| 14 | + <h1>Homepage</h1> |
67 | 15 | </div> |
68 | 16 | ); |
69 | | -} |
| 17 | +}; |
70 | 18 |
|
71 | | -function BlogPage() { |
72 | | - delay(0.5); |
| 19 | +const Blog = () => { |
| 20 | + delay(1); |
73 | 21 |
|
74 | 22 | 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> |
77 | 61 | </div> |
78 | 62 | ); |
79 | 63 | } |
|
0 commit comments