-
Notifications
You must be signed in to change notification settings - Fork 47.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flight] Implement useId hook #24172
Changes from all commits
1b7aaab
baa298b
b224f07
2c4a074
aa490a3
d72d44d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,21 @@ | |
*/ | ||
|
||
import type {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactInternalTypes'; | ||
import type {Request} from './ReactFlightServer'; | ||
import type {ReactServerContext} from 'shared/ReactTypes'; | ||
import {REACT_SERVER_CONTEXT_TYPE} from 'shared/ReactSymbols'; | ||
import {readContext as readContextImpl} from './ReactFlightNewContext'; | ||
|
||
let currentRequest = null; | ||
|
||
export function prepareToUseHooksForRequest(request: Request) { | ||
currentRequest = request; | ||
} | ||
|
||
export function resetHooksForRequest() { | ||
currentRequest = null; | ||
} | ||
|
||
Comment on lines
+16
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sebmarkbage there is already setCurrentCache behavior that I could consolidate since they are set and reset at the same points in performWork. For setting the cache I noticed that the previous cache is restored but my understanding of the code is the previous cache will always be null (meaning you cannot have a second performWork start before a first performWork finishes) If the prevCache restoration is important it makes consolidation of multiple things to set awkward but we could move to just reading the cache from the request given it is set for the duration of performWork and holds the cache |
||
function readContext<T>(context: ReactServerContext<T>): T { | ||
if (__DEV__) { | ||
if (context.$$typeof !== REACT_SERVER_CONTEXT_TYPE) { | ||
|
@@ -61,7 +72,7 @@ export const Dispatcher: DispatcherType = { | |
useLayoutEffect: (unsupportedHook: any), | ||
useImperativeHandle: (unsupportedHook: any), | ||
useEffect: (unsupportedHook: any), | ||
useId: (unsupportedHook: any), | ||
useId, | ||
useMutableSource: (unsupportedHook: any), | ||
useSyncExternalStore: (unsupportedHook: any), | ||
useCacheRefresh(): <T>(?() => T, ?T) => void { | ||
|
@@ -91,3 +102,12 @@ export function setCurrentCache(cache: Map<Function, mixed> | null) { | |
export function getCurrentCache() { | ||
return currentCache; | ||
} | ||
|
||
function useId(): string { | ||
if (currentRequest === null) { | ||
throw new Error('useId can only be used while React is rendering'); | ||
} | ||
const id = currentRequest.identifierCount++; | ||
// use 'S' for Flight components to distinguish from 'R' and 'r' in Fizz/Client | ||
return ':' + currentRequest.identifierPrefix + 'S' + id.toString(32) + ':'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,13 @@ import { | |
isModuleReference, | ||
} from './ReactFlightServerConfig'; | ||
|
||
import {Dispatcher, getCurrentCache, setCurrentCache} from './ReactFlightHooks'; | ||
import { | ||
Dispatcher, | ||
getCurrentCache, | ||
prepareToUseHooksForRequest, | ||
resetHooksForRequest, | ||
setCurrentCache, | ||
} from './ReactFlightHooks'; | ||
import { | ||
pushProvider, | ||
popProvider, | ||
|
@@ -102,14 +108,12 @@ export type Request = { | |
writtenSymbols: Map<Symbol, number>, | ||
writtenModules: Map<ModuleKey, number>, | ||
writtenProviders: Map<string, number>, | ||
identifierPrefix: string, | ||
identifierCount: number, | ||
onError: (error: mixed) => void, | ||
toJSON: (key: string, value: ReactModel) => ReactJSONValue, | ||
}; | ||
|
||
export type Options = { | ||
onError?: (error: mixed) => void, | ||
}; | ||
|
||
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher; | ||
|
||
function defaultErrorHandler(error: mixed) { | ||
|
@@ -126,6 +130,7 @@ export function createRequest( | |
bundlerConfig: BundlerConfig, | ||
onError: void | ((error: mixed) => void), | ||
context?: Array<[string, ServerContextJSONValue]>, | ||
identifierPrefix?: string, | ||
): Request { | ||
const pingedSegments = []; | ||
const request = { | ||
|
@@ -143,6 +148,8 @@ export function createRequest( | |
writtenSymbols: new Map(), | ||
writtenModules: new Map(), | ||
writtenProviders: new Map(), | ||
identifierPrefix: identifierPrefix || '', | ||
identifierCount: 1, | ||
onError: onError === undefined ? defaultErrorHandler : onError, | ||
toJSON: function(key: string, value: ReactModel): ReactJSONValue { | ||
return resolveModelToJSON(request, this, key, value); | ||
|
@@ -826,6 +833,7 @@ function performWork(request: Request): void { | |
const prevCache = getCurrentCache(); | ||
ReactCurrentDispatcher.current = Dispatcher; | ||
setCurrentCache(request.cache); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of setting up two different contextual variables (and more to come) we should just use the request for the Cache too. That can be a follow up but should fast follow. While we're add it we should also put the Cache behind the |
||
prepareToUseHooksForRequest(request); | ||
|
||
try { | ||
const pingedSegments = request.pingedSegments; | ||
|
@@ -843,6 +851,7 @@ function performWork(request: Request): void { | |
} finally { | ||
ReactCurrentDispatcher.current = prevDispatcher; | ||
setCurrentCache(prevCache); | ||
resetHooksForRequest(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a test for how
useId
would work for sever components that have children that are client components and need the same ID?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I'm misunderstanding some finer point but my expectation is that if you use
useId
in Flight you will if necessary pass that id as props to client components like any other prop when needed for some purposeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just checking if there's a test for it already!