Skip to content

Commit

Permalink
fix(react): capitalize and make PageContext generic or null, forc…
Browse files Browse the repository at this point in the history
…e `usePage` return type explicit
  • Loading branch information
jrson83 committed Apr 23, 2024
1 parent c4bd85a commit 8d7ffbc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/react/src/PageContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Page, PageProps } from '@inertiajs-revamped/core'
import { createContext } from 'react'

const pageContext = createContext(undefined)
pageContext.displayName = 'InertiaPageContext'
const PageContext = createContext<Page<PageProps> | null>(null)
PageContext.displayName = 'InertiaPageContext'

export default pageContext
export default PageContext
7 changes: 4 additions & 3 deletions packages/react/src/usePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { useContext } from 'react'
import PageContext from './PageContext'

export default function usePage<
TPageProps extends PageProps = PageProps,
>(): Page<TPageProps> {
const page = useContext(PageContext)
SharedProps extends PageProps = PageProps,
>(): Page<SharedProps> {
// @ts-expect-error Satisfy TS if branch on line below
const page: Page<SharedProps> = useContext(PageContext)

if (!page) {
throw new Error('usePage must be used within the Inertia component')
Expand Down

0 comments on commit 8d7ffbc

Please sign in to comment.