Skip to content

Commit b9ba41a

Browse files
Switch useContext to use in usePage() hook (#2680)
* refactor: replace useContext with use for PageContext * refactor: update usePage to support React <19 compatibility * format * Update comment --------- Co-authored-by: Pascal Baljet <[email protected]>
1 parent 4ca05fc commit b9ba41a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

packages/react/src/usePage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Page, PageProps, SharedPageProps } from '@inertiajs/core'
2-
import { useContext } from 'react'
2+
import React from 'react'
33
import PageContext from './PageContext'
44

55
export default function usePage<TPageProps extends PageProps = PageProps>(): Page<TPageProps & SharedPageProps> {
6-
const page = useContext(PageContext)
6+
// React.use() was introduced in React 19, fallback to React.useContext() for earlier versions
7+
const page = typeof React.use === 'function' ? React.use(PageContext) : React.useContext(PageContext)
78

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

0 commit comments

Comments
 (0)