Skip to content

Commit 705e99b

Browse files
committed
Add pathless routes feature and complete
1 parent c479045 commit 705e99b

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Vite + React</title>
7+
<meta name="description" content="This is built for learning the React Router 6.21">
8+
<title>React Router Template</title>
89
</head>
910
<body>
1011
<div id="root"></div>

src/main.jsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
33
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
4-
// import Root from './routes/root';
54
import Index from './routes';
65
import ErrorPage from './error-page';
76
import Contact from './routes/contact';
87
import Root, {
98
loader as rootLoader,
109
action as rootAction,
1110
} from './routes/root';
12-
import { loader as contactLoader, action as contactAction } from './routes/contact';
11+
import {
12+
loader as contactLoader,
13+
action as contactAction,
14+
} from './routes/contact';
1315
import './index.css';
1416
import EditContact from './routes/edit';
1517
import { action as editAction } from './routes/edit';
16-
// import DeleteContact from './routes/destroy'
1718
import { action as deleteAction } from './routes/destroy';
1819

1920
const router = createBrowserRouter([
@@ -25,11 +26,11 @@ const router = createBrowserRouter([
2526
action: rootAction,
2627
children: [
2728
{ index: true, element: <Index /> },
28-
{
29+
{
2930
path: 'contacts/:contactId',
3031
element: <Contact />,
3132
loader: contactLoader,
32-
action: contactAction
33+
action: contactAction,
3334
},
3435
{
3536
path: 'contacts/:contactId/edit',
@@ -43,9 +44,38 @@ const router = createBrowserRouter([
4344
action: deleteAction,
4445
errorElement: <div>Oops! There was an error.</div>,
4546
},
47+
{
48+
errorElement: <ErrorPage />,
49+
children: [
50+
{ index: true, element: <Index /> },
51+
{
52+
path: 'contacts/:contactId',
53+
element: <Contact />,
54+
loader: contactLoader,
55+
action: contactAction,
56+
},
57+
],
58+
},
4659
],
4760
},
4861
]);
62+
/* Also we can configure the routes with JSX
63+
using by importing createRoutesFromElements in react-router-dom.
64+
for example:
65+
const router = createBrowserRouter(
66+
createRoutesFromElements(
67+
<Route
68+
path="/"
69+
element={<Root />}
70+
loader={rootLoader}
71+
action={rootAction}
72+
errorElement={<ErrorPage />}
73+
>
74+
{...child routes configurations}
75+
</Route>
76+
)
77+
)
78+
*/
4979

5080
ReactDOM.createRoot(document.getElementById('root')).render(
5181
<React.StrictMode>

src/routes/contact.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { getContact, updateContact } from "../contacts";
33

44
export const loader = async({params}) => {
55
const contact = await getContact(params.contactId)
6+
if(!contact) {
7+
throw new Error("", {
8+
status: 404,
9+
statusText: "Not found"
10+
})
11+
}
612
return {contact}
713
}
814

@@ -88,6 +94,10 @@ function Favorite({ contact }) {
8894
// yes, this is a `let` for later
8995
const fetcher = useFetcher()
9096
let favorite = contact.favorite;
97+
// Optimistic UI
98+
if(fetcher.formData) {
99+
favorite = fetcher.formData.get('favorite') === true
100+
}
91101
return (
92102
<fetcher.Form method="post">
93103
<button

0 commit comments

Comments
 (0)