Skip to content

Commit 7ef700d

Browse files
authored
Add a new useLoadableQuery hook (#11300)
1 parent 83661c3 commit 7ef700d

File tree

6 files changed

+228
-83
lines changed

6 files changed

+228
-83
lines changed

src/testing/internal/profile/Render.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export interface Render<Snapshot> extends BaseRender {
6262
* ```
6363
*/
6464
withinDOM: () => SyncScreen;
65+
66+
renderedComponents: Array<string | React.ComponentType>;
6567
}
6668

6769
/** @internal */
@@ -77,7 +79,8 @@ export class RenderInstance<Snapshot> implements Render<Snapshot> {
7779
constructor(
7880
baseRender: BaseRender,
7981
public snapshot: Snapshot,
80-
private stringifiedDOM: string | undefined
82+
private stringifiedDOM: string | undefined,
83+
public renderedComponents: Array<string | React.ComponentType>
8184
) {
8285
this.id = baseRender.id;
8386
this.phase = baseRender.phase;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as React from "react";
2+
3+
export interface ProfilerContextValue {
4+
renderedComponents: Array<React.ComponentType | string>;
5+
}
6+
7+
const ProfilerContext = React.createContext<ProfilerContextValue | undefined>(
8+
undefined
9+
);
10+
11+
export function ProfilerContextProvider({
12+
children,
13+
value,
14+
}: {
15+
children: React.ReactNode;
16+
value: ProfilerContextValue;
17+
}) {
18+
const parentContext = useProfilerContext();
19+
20+
if (parentContext) {
21+
throw new Error("Profilers should not be nested in the same tree");
22+
}
23+
24+
return (
25+
<ProfilerContext.Provider value={value}>
26+
{children}
27+
</ProfilerContext.Provider>
28+
);
29+
}
30+
31+
export function useProfilerContext() {
32+
return React.useContext(ProfilerContext);
33+
}

src/testing/internal/profile/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
export type {
22
NextRenderOptions,
3+
Profiler,
34
ProfiledComponent,
45
ProfiledHook,
56
} from "./profile.js";
6-
export { profile, profileHook, WaitForRenderTimeoutError } from "./profile.js";
7+
export {
8+
createProfiler,
9+
profile,
10+
profileHook,
11+
useTrackRenders,
12+
WaitForRenderTimeoutError,
13+
} from "./profile.js";
714

815
export type { SyncScreen } from "./Render.js";

0 commit comments

Comments
 (0)