diff --git a/packages/react-router/__tests__/useOutlet-test.tsx b/packages/react-router/__tests__/useOutlet-test.tsx index 17a9133e91..f488f975bc 100644 --- a/packages/react-router/__tests__/useOutlet-test.tsx +++ b/packages/react-router/__tests__/useOutlet-test.tsx @@ -28,6 +28,54 @@ describe("useOutlet", () => { expect(renderer.toJSON()).toBeNull(); }); + + it("renders the fallback", () => { + function Home() { + let outlet = useOutlet(); + return
{outlet ? "outlet" : "no outlet"}
; + } + + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + } /> + + + ); + }); + + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+ no outlet +
+ `); + }); + + it("renders the fallback with context provided", () => { + function Home() { + let outlet = useOutlet({ some: "context" }); + return
{outlet ? "outlet" : "no outlet"}
; + } + + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + } /> + + + ); + }); + + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+ no outlet +
+ `); + }); }); describe("when there is a child route", () => { @@ -55,9 +103,61 @@ describe("useOutlet", () => { `); }); + + it("returns an element when no context", () => { + function Home() { + let outlet = useOutlet(); + return
{outlet ? "outlet" : "no outlet"}
; + } + + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + }> + index} /> + + + + ); + }); + + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+ outlet +
+ `); + }); + + it("returns an element when context", () => { + function Home() { + let outlet = useOutlet({ some: "context" }); + return
{outlet ? "outlet" : "no outlet"}
; + } + + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + + }> + index} /> + + + + ); + }); + + expect(renderer.toJSON()).toMatchInlineSnapshot(` +
+ outlet +
+ `); + }); }); - describe("when there is no context", () => { + describe("OutletContext when there is no context", () => { it("returns null", () => { function Users() { return useOutlet(); @@ -98,7 +198,7 @@ describe("useOutlet", () => { }); }); - describe("when there is context", () => { + describe("OutletContext when there is context", () => { it("returns the context", () => { function Users() { return useOutlet([ diff --git a/packages/react-router/index.tsx b/packages/react-router/index.tsx index f90915aad4..d0fa995f0e 100644 --- a/packages/react-router/index.tsx +++ b/packages/react-router/index.tsx @@ -579,9 +579,12 @@ export function useOutletContext(): Context { */ export function useOutlet(context?: unknown): React.ReactElement | null { let outlet = React.useContext(RouteContext).outlet; - return ( - {outlet} - ); + if (outlet) { + return ( + {outlet} + ); + } + return outlet; } /**