Skip to content

Commit e937ea1

Browse files
authored
feat(preset): add react-router & react-router-dom (unplugin#110)
1 parent 2a64fcc commit e937ea1

File tree

11 files changed

+169
-3
lines changed

11 files changed

+169
-3
lines changed

examples/vite-react/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
},
99
"dependencies": {
1010
"react": "^17.0.2",
11-
"react-dom": "^17.0.2"
11+
"react-dom": "^17.0.2",
12+
"react-router": "^6.2.1",
13+
"react-router-dom": "^6.2.1"
1214
},
1315
"devDependencies": {
1416
"@iconify-json/logos": "*",

examples/vite-react/src/App.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// eslint-disable-next-line no-use-before-define
22
import React from 'react'
3+
import MainLayout from './layouts/MainLayout'
4+
import PageA from './views/PageA'
5+
import PageB from './views/PageB'
36

47
function App() {
58
const [count, setCount] = useState(0)
@@ -14,6 +17,12 @@ function App() {
1417
</button>
1518
</p>
1619
</header>
20+
<Routes>
21+
<Route path="/" element={<MainLayout />} >
22+
<Route path="/list" element={<PageA />} />
23+
<Route path="/detail/:id" element={<PageB />} />
24+
</Route>
25+
</Routes>
1726
</div>
1827
)
1928
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
3+
const MainLayout = () => {
4+
return (
5+
<div>
6+
<h1>Main Layout Header</h1>
7+
<nav>
8+
<Link to="/list">List</Link>
9+
</nav>
10+
<Outlet />
11+
<h1>Main Layout Footer</h1>
12+
</div>
13+
)
14+
}
15+
16+
export default MainLayout

examples/vite-react/src/main.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// eslint-disable-next-line no-use-before-define
22
import React from 'react'
33
import ReactDOM from 'react-dom'
4+
import { BrowserRouter } from 'react-router-dom'
45
import App from './App'
56

67
ReactDOM.render(
78
<React.StrictMode>
8-
<App />
9+
<BrowserRouter>
10+
<App />
11+
</BrowserRouter>
912
</React.StrictMode>,
1013
document.getElementById('root'),
1114
)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
3+
const PageA = () => {
4+
return (
5+
<div>
6+
<main>
7+
<h2>Welcome to the homepage!</h2>
8+
<p>You can do this, I believe in you.</p>
9+
</main>
10+
<ul>
11+
{[13, 14, 15].map(n => (
12+
<li key={n}>
13+
<nav>
14+
<Link to={`/detail/${new Date().getTime()}?q=${new Date().getTime() % n}`}>Detail-{n}</Link>
15+
</nav>
16+
</li>
17+
))}
18+
</ul>
19+
</div>
20+
)
21+
}
22+
23+
export default PageA
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
const PageB = () => {
4+
const location = useLocation()
5+
const { id } = useParams()
6+
const [query] = useSearchParams()
7+
return (
8+
<div>
9+
<main>
10+
<h2>Who are we?</h2>
11+
<p>
12+
That feels like an existential question, don&apos;t you
13+
think?
14+
</p>
15+
<p>path: {location.pathname}</p>
16+
<p>params id: {id}</p>
17+
<p>query q: {query.get('q')}</p>
18+
</main>
19+
<nav>
20+
<Link to="/">Home</Link>
21+
</nav>
22+
</div>
23+
)
24+
}
25+
26+
export default PageB

examples/vite-react/vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
jsx: 'react',
1414
}),
1515
AutoImport({
16-
imports: 'react',
16+
imports: ['react', 'react-router-dom'],
1717
dts: './src/auto-imports.d.ts',
1818
resolvers: [
1919
IconsResolver({

pnpm-lock.yaml

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/presets/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import pinia from './pinia'
44
import preact from './preact'
55
import quasar from './quasar'
66
import react from './react'
7+
import reactRouter from './react-router'
8+
import reactRouterDom from './react-router-dom'
79
import {
810
svelte,
911
svelteAnimate,
@@ -33,6 +35,8 @@ export const presets = {
3335
'preact': preact,
3436
'quasar': quasar,
3537
'react': react,
38+
'react-router': reactRouter,
39+
'react-router-dom': reactRouterDom,
3640
'svelte': svelte,
3741
'svelte/animate': svelteAnimate,
3842
'svelte/easing': svelteEasing,

src/presets/react-router-dom.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { ImportsMap } from '../types'
2+
import { ReactRouterHooks } from './react-router'
3+
4+
/**
5+
* Only compatible with React Router Dom v6.
6+
*/
7+
export default <ImportsMap>({
8+
'react-router-dom': [
9+
...ReactRouterHooks,
10+
11+
// react-router-dom only hooks
12+
'useLinkClickHandler',
13+
'useSearchParams',
14+
15+
// react-router-dom Component
16+
17+
// call once in general
18+
// 'BrowserRouter',
19+
// 'HashRouter',
20+
// 'MemoryRouter',
21+
22+
'Link',
23+
'NavLink',
24+
'Navigate',
25+
'Outlet',
26+
'Route',
27+
'Routes',
28+
],
29+
})

src/presets/react-router.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { ImportsMap } from '../types'
2+
3+
/**
4+
* Only compatible with React Router v6.
5+
*/
6+
export const ReactRouterHooks = [
7+
'useOutletContext',
8+
'useHref',
9+
'useInRouterContext',
10+
'useLocation',
11+
'useNavigationType',
12+
'useNavigate',
13+
'useOutlet',
14+
'useParams',
15+
'useResolvedPath',
16+
'useRoutes',
17+
]
18+
19+
export default <ImportsMap>({
20+
'react-router': [
21+
...ReactRouterHooks,
22+
],
23+
})

0 commit comments

Comments
 (0)