Skip to content

Commit bc80dc3

Browse files
committed
Add Contact component and configure initial router
1 parent e949d5d commit bc80dc3

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/error-page.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useRouteError } from "react-router-dom";
2+
3+
export default function ErrorPage() {
4+
const error = useRouteError();
5+
console.error(error);
6+
7+
return (
8+
<div id="error-page">
9+
<h1>Oops!</h1>
10+
<p>Sorry, an unexpected error has occurred.</p>
11+
<p>
12+
<i>{error.statusText || error.message}</i>
13+
</p>
14+
</div>
15+
);
16+
}

src/main.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
3-
import App from './App.jsx'
3+
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
4+
import Root from './routes/root'
5+
import ErrorPage from './error-page'
6+
// import App from './App.jsx'
47
import './index.css'
58

9+
const router = createBrowserRouter([
10+
{ path: '/',
11+
element: <Root/>,
12+
errorElement: <ErrorPage />,
13+
}
14+
])
15+
616
ReactDOM.createRoot(document.getElementById('root')).render(
717
<React.StrictMode>
8-
<App />
18+
<RouterProvider router={router} />
919
</React.StrictMode>,
1020
)

src/routes/root.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export default function Root() {
2+
return (
3+
<>
4+
<div id="sidebar">
5+
<h1>React Router Contacts</h1>
6+
<div>
7+
<form id="search-form" role="search">
8+
<input
9+
id="q"
10+
aria-label="Search contacts"
11+
placeholder="Search"
12+
type="search"
13+
name="q"
14+
/>
15+
<div
16+
id="search-spinner"
17+
aria-hidden
18+
hidden={true}
19+
/>
20+
<div
21+
className="sr-only"
22+
aria-live="polite"
23+
></div>
24+
</form>
25+
<form method="post">
26+
<button type="submit">New</button>
27+
</form>
28+
</div>
29+
<nav>
30+
<ul>
31+
<li>
32+
<a href={`/contacts/1`}>Your Name</a>
33+
</li>
34+
<li>
35+
<a href={`/contacts/2`}>Your Friend</a>
36+
</li>
37+
</ul>
38+
</nav>
39+
</div>
40+
<div id="detail"></div>
41+
</>
42+
);
43+
}

0 commit comments

Comments
 (0)