Skip to content

Commit 31f437b

Browse files
committed
React Router, styled-components
1 parent 59e6487 commit 31f437b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+35624
-16
lines changed

20230201/react-1/src/App.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1+
import Header from './components/Header';
2+
import Footer from './components/Footer';
3+
4+
import AboutPage from './pages/AboutPage';
5+
import HomePage from './pages/HomePage';
6+
import LoginPage from './pages/LoginPage';
7+
8+
const pages = {
9+
'/': HomePage,
10+
'/about': AboutPage,
11+
'/login': LoginPage,
12+
};
13+
114
export default function App() {
15+
const { pathname } = window.location;
16+
17+
const Page = Reflect.get(pages, pathname) || HomePage;
18+
219
return (
3-
<p>
4-
Hello, world!
5-
</p>
20+
<div>
21+
<Header />
22+
<main>
23+
<Page />
24+
</main>
25+
<Footer />
26+
</div>
627
);
728
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function Footer() {
2+
return (
3+
<footer>
4+
<hr />
5+
Footer
6+
</footer>
7+
);
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function Header() {
2+
return (
3+
<header>
4+
<nav>
5+
<ul>
6+
<li>
7+
<a href="/">Home</a>
8+
</li>
9+
<li>
10+
<a href="/about">About</a>
11+
</li>
12+
<li>
13+
<a href="/login">Login</a>
14+
</li>
15+
</ul>
16+
</nav>
17+
</header>
18+
);
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function AboutPage() {
2+
return (
3+
<div>
4+
<h1>About Demo App</h1>
5+
<p>Hello, world!</p>
6+
</div>
7+
);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function HomePage() {
2+
return (
3+
<div>
4+
<h1>Welcome!</h1>
5+
<p>Hello, how are you?</p>
6+
</div>
7+
);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function LoginPage() {
2+
return (
3+
<div>
4+
<h1>Log in</h1>
5+
<p>Username / Password</p>
6+
</div>
7+
);
8+
}

20230201/react-2/package-lock.json

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

20230201/react-2/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0",
21+
"react-router-dom": "^6.8.0",
2122
"reflect-metadata": "^0.1.13",
2223
"tsyringe": "^4.7.0",
2324
"usehooks-ts": "^2.9.1",

20230201/react-2/src/App.test.tsx

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
import { render } from '@testing-library/react';
1+
import { render, screen } from '@testing-library/react';
2+
3+
import { MemoryRouter } from 'react-router-dom';
24

35
import App from './App';
46

5-
test('App', () => {
6-
render(<App />);
7+
const context = describe;
8+
9+
describe('App', () => {
10+
function renderApp(path: string) {
11+
render((
12+
<MemoryRouter initialEntries={[path]}>
13+
<App />
14+
</MemoryRouter>
15+
));
16+
}
17+
18+
context('when the current page is "/"', () => {
19+
it('renders the home page', () => {
20+
renderApp('/');
21+
22+
screen.getByText(/Welcome/);
23+
});
24+
});
25+
26+
context('when the current page is "/products"', () => {
27+
it('renders the product list page', () => {
28+
renderApp('/products');
29+
30+
screen.getByText(/Product list/);
31+
});
32+
});
33+
34+
context('when the current page is "/about"', () => {
35+
it('renders the about page', () => {
36+
renderApp('/about');
37+
38+
screen.getByText(/About Demo App/);
39+
});
40+
});
41+
42+
context('when the current page is "/login"', () => {
43+
it('renders the login page', () => {
44+
renderApp('/login');
45+
46+
screen.getByText(/Username/);
47+
});
48+
});
749
});

20230201/react-2/src/App.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
import Greeting from './components/Greeting';
1+
import { Routes, Route } from 'react-router-dom';
2+
3+
import Header from './components/Header';
4+
import Footer from './components/Footer';
5+
6+
import AboutPage from './pages/AboutPage';
7+
import ProductsPage from './pages/ProductsPage';
8+
import HomePage from './pages/HomePage';
9+
import LoginPage from './pages/LoginPage';
210

311
export default function App() {
412
return (
513
<div>
6-
<Greeting />
14+
<Header />
15+
<main>
16+
<Routes>
17+
<Route path="/" element={<HomePage />} />
18+
<Route path="/products" element={<ProductsPage />} />
19+
<Route path="/about" element={<AboutPage />} />
20+
<Route path="/login" element={<LoginPage />} />
21+
</Routes>
22+
</main>
23+
<Footer />
724
</div>
825
);
926
}

0 commit comments

Comments
 (0)