-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d45838
commit fc3a1f5
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# react router | ||
|
||
## example | ||
|
||
```js | ||
import * as React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import { | ||
createBrowserRouter, | ||
RouterProvider, | ||
Route, | ||
Link, | ||
} from 'react-router-dom' | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
path: '/', | ||
element: <Home/>, | ||
children: [ | ||
{ | ||
path: 'contact', | ||
element: <Contact/> | ||
}, | ||
{ | ||
path: 'dashboard', | ||
element: <Dashboard/>, | ||
loader: ({request}) => { | ||
return fetch('/api/dashboard.json', { | ||
signal: request.signal | ||
}) | ||
} | ||
}, | ||
{ | ||
element: <AuthLayout/>, | ||
children: [ | ||
{ | ||
path: 'login', | ||
element: <Login/> | ||
loader: redirectIfUser, | ||
}, | ||
{ | ||
path: 'logout', | ||
action: logoutUser | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
path: 'about', | ||
element: <About/> | ||
} | ||
]) | ||
|
||
createRoot(document.getElementById('root')).render( | ||
<RouterProvider router={router}/> | ||
) | ||
``` | ||
|
||
## 速记 | ||
|
||
react-router@6教程使用vite构建react项目 | ||
|
||
使用Outlet元素实现嵌套路由,即在需要后代路由的组件上的某个地方加上Outlet元素即可 | ||
|
||
使用loader属性(与path、element等同级)将数据导入到路由组件(比如Root)上,(在Root)使用useLoaderData方法访问导入的数据 | ||
|
||
使用errorElement属性(与path等同级)捕获路由错误,该属性值的组件可复用 |