1
1
import React from 'react' ;
2
2
import ReactDOM from 'react-dom/client' ;
3
3
import { createBrowserRouter , RouterProvider } from 'react-router-dom' ;
4
- // import Root from './routes/root';
5
4
import Index from './routes' ;
6
5
import ErrorPage from './error-page' ;
7
6
import Contact from './routes/contact' ;
8
7
import Root , {
9
8
loader as rootLoader ,
10
9
action as rootAction ,
11
10
} 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' ;
13
15
import './index.css' ;
14
16
import EditContact from './routes/edit' ;
15
17
import { action as editAction } from './routes/edit' ;
16
- // import DeleteContact from './routes/destroy'
17
18
import { action as deleteAction } from './routes/destroy' ;
18
19
19
20
const router = createBrowserRouter ( [
@@ -25,11 +26,11 @@ const router = createBrowserRouter([
25
26
action : rootAction ,
26
27
children : [
27
28
{ index : true , element : < Index /> } ,
28
- {
29
+ {
29
30
path : 'contacts/:contactId' ,
30
31
element : < Contact /> ,
31
32
loader : contactLoader ,
32
- action : contactAction
33
+ action : contactAction ,
33
34
} ,
34
35
{
35
36
path : 'contacts/:contactId/edit' ,
@@ -43,9 +44,38 @@ const router = createBrowserRouter([
43
44
action : deleteAction ,
44
45
errorElement : < div > Oops! There was an error.</ div > ,
45
46
} ,
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
+ } ,
46
59
] ,
47
60
} ,
48
61
] ) ;
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
+ */
49
79
50
80
ReactDOM . createRoot ( document . getElementById ( 'root' ) ) . render (
51
81
< React . StrictMode >
0 commit comments