Solid Navigator is a library that is inspired by vue-router and @solidjs/router.
Install it:
npm i solid-navigator
# or
pnpm add solid-navigator
Use it:
import { Router, Route, Outlet, A, Navigate, useNavigate, useLocation, useParams, useMatch, useSearchParams } from 'solid-navigator'
const navigate = useNavigate();
navigate("/app", {replace: true})
// path: /app?id=1
const location = useLocation();
{
query: {id: string}
search: string
pathname: string
hash: string
}
// path: /chats/:id
const params = useParams<{id: string}>();
{
id: string
}
// path: /chats/1234
const match = useMatch(() => "/chats/1234");
{
path: "/chats/1234"
params: {}
} | null
// path: /chats?id=1234
const [params, setParams] = useSearchParams();
params = {
id: "1234"
} | null
setParams({id: "5678", hello: "Bye"}) // path: /chats?id=5678&hello=Bye
const Root = () => {
return (
<div>
<h1>Header</h1>
<Outlet/>
</div>
)
}
const Main = () => {
return (
<Router root={Root}>
// Routes go here
</Router>
)
}
const Main = () => {
const AppComponent = () => {
return (
<div>
<div><Outlet name="drawer"/></div>
<div><Outlet name="content"/></div>
</div>
)
}
return (
<Router root={Root}>
<Route path="/" component={AppComponent}>
<Route
components={{
drawer: Drawer,
content: Content
}}
/>
</Route>
</Router>
)
}
const App = () => {
return (
<A href="/" replace />
)
}
const App = () => {
return (
<Navigate to="/" />
)
}