Skip to content

Commit

Permalink
Added match component
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Jan 19, 2024
1 parent 0003c53 commit 6e0e093
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Root = () => {
const [searchParams, setSearchParams] = useSearchParams()
const location = useLocation()
const navigate = useNavigate()
const match = useMatch(() => '/app/:id ')
const match = useMatch(() => '/app/:id?')
const params = useParams()

createEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solid-navigator",
"version": "0.2.5",
"version": "0.3.0",
"description": "Solid Navigator is a library that is inspired by vue router and solid router.",
"license": "MIT",
"author": "SupertigerDev",
Expand Down
4 changes: 2 additions & 2 deletions src/Outlet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export const Outlet = (props: { children?: string; name?: string }) => {
return component
})

const MemodComponent = createMemo(() => {
const MemoComponent = createMemo(() => {
return component()()
})

return <>{MemodComponent}</>
return <>{MemoComponent}</>
}
18 changes: 17 additions & 1 deletion src/Route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSX, children, mergeProps } from 'solid-js'
import { Component, JSX, children, createMemo, mergeProps } from 'solid-js'
import { useRouterContext } from './Router'

export interface RouteObject {
path: string
Expand All @@ -21,3 +22,18 @@ export const Route = (props: Omit<RouteObject, 'children'> & { children?: JSX.El
},
}) as unknown as JSX.Element
}


export const matchComponent = (name: () => string) => {
const context = useRouterContext()
const matched = () => context.matched()

const component = createMemo(() => {
const components = context.matched()?.route.components || context.matched()?.route.mergedComponents || {}
return components[name()]
})

return createMemo(() => {
return component()()
}) as Component
}
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NavigateOptions, useNavigate, Navigate, useSearchParams } from './navig

import { Outlet } from './Outlet'

import { Route, RouteObject } from './Route'
import { Route, RouteObject, matchComponent } from './Route'

import { Router, useLocation, useParams, RouterProps } from './Router'

Expand All @@ -21,4 +21,5 @@ export {
Navigate,
useMatch,
useSearchParams,
matchComponent
}
1 change: 1 addition & 0 deletions src/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ export const useSearchParams = () => {

return [context.location.query, updateQuery] as const
}

0 comments on commit 6e0e093

Please sign in to comment.