Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/react-router/src/awaited.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as React from 'react'

import { TSR_DEFERRED_PROMISE, defer } from '@tanstack/router-core'
import { reactUse } from './utils'

export type AwaitOptions<T> = {
promise: Promise<T>
}

/** Suspend until a deferred promise resolves or rejects and return its data. */
export function useAwaited<T>({ promise: _promise }: AwaitOptions<T>): T {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (React.use) {
const data = React.use(_promise)
if (reactUse) {
const data = reactUse(_promise)
return data
}
const promise = defer(_promise)
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router/src/lazyRouteComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'
import { isModuleNotFoundError } from '@tanstack/router-core'
import { reactUse } from './utils'
import type { AsyncRouteComponent } from './route'

/**
Expand Down Expand Up @@ -79,9 +80,8 @@ export function lazyRouteComponent<
}

if (!comp) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (React.use) {
React.use(load())
if (reactUse) {
reactUse(load())
} else {
throw load()
}
Expand Down
14 changes: 14 additions & 0 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import * as React from 'react'

// Safe version of React.use() that will not cause compilation errors against
// React 18 with Webpack, which statically analyzes imports and fails when it
// sees React.use referenced (since 'use' is not exported from React 18).
// This uses a dynamic string lookup to avoid the static analysis.
const REACT_USE = 'use'

/**
* React.use if available (React 19+), undefined otherwise.
* Use dynamic lookup to avoid Webpack compilation errors with React 18.
*/
export const reactUse:
| (<T>(usable: Promise<T> | React.Context<T>) => T)
| undefined = (React as any)[REACT_USE]

export function useStableCallback<T extends (...args: Array<any>) => any>(
fn: T,
): T {
Expand Down
Loading