29
loading...
This website collects cookies to deliver better user experience
react-router official nested routing sample code has been rolled back to centralized
https://reactrouter.com/docs/en/v6/getting-started/overview#nested-routes
Note: The current lib is not compiled, your build tool must support compiling tsx (such as vite or esbuild).
pnpm rm react-router-dom react-router
pnpm i @liuli-util/react-router
//router.ts
import { RouteConfig, createHashHistory } from '@liuli-util/react-router'
import { HomeView } from '../views/HomeView'
import { HelloWorld } from '../views/HelloWorld'
export const routeList: RouteConfig[] = [
{ path: '/', component: HomeView },
{ path: '/hello-world', component: HelloWorld },
]
export const history = createHashHistory()
<ReactRouter />
as the root component and use the layout component//main.tsx
import React from 'react'
import ReactDOM from 'react-dom'
import { ReactRouter } from '@liuli-util/react-router'
import { history, routeList } from './constants/router'
ReactDOM.render(
<React.StrictMode>
<ReactRouter history={history} routes={routeList} />
</React.StrictMode>,
document.getElementById('root'),
)
//router.ts
import { RouteConfig, createHashHistory } from '@liuli-util/react-router'
import { HomeView } from '../views/HomeView'
import { HelloWorld } from '../views/HelloWorld'
export const routeList: RouteConfig[] = [
{
path: '/',
component: Layout,
children: [
{ path: '/', component: HomeView },
{ path: '/hello-world', component: HelloWorld },
],
},
]
export const history = createHashHistory()
import * as React from 'react'
import { RouterView } from '@liuli-util/react-router'
export const Layout: React.FC = () => (
<div>
<h2>Layout Header</h2>
<RouterView />
</div>
)
() => import('path')
export const routeList: RouteConfig[] = [
{ path: '/', component: () => import('../views/HomeView') },
{ path: '/hello-world', component: () => import('../views/HelloWorld') },
]
import { history } from './router'
//Jump
history.push('/')
history.push({
pathname: '/',
query: { name: 'liuli' },
})
history.back()
//Get some information about the current route
history.location