Skip to content
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

Deferred API updates #9070

Merged
merged 15 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions examples/data-router/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import {
type ActionFunction,
type Deferrable,
type LoaderFunction,
DataBrowserRouter,
Deferred,
Expand Down Expand Up @@ -238,11 +237,11 @@ function Todo() {
interface DeferredRouteLoaderData {
critical1: string;
critical2: string;
lazyResolved: Deferrable<string>;
lazy1: Deferrable<string>;
lazy2: Deferrable<string>;
lazy3: Deferrable<string>;
lazyError: Deferrable<string>;
lazyResolved: Promise<string>;
lazy1: Promise<string>;
lazy2: Promise<string>;
lazy3: Promise<string>;
lazyError: Promise<string>;
}

const rand = () => Math.round(Math.random() * 100);
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export type {
ActionFunctionArgs,
DataMemoryRouterProps,
DataRouteMatch,
Deferrable,
DeferredProps,
Fetcher,
Hash,
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-native/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type {
ActionFunctionArgs,
DataMemoryRouterProps,
DataRouteMatch,
Deferrable,
DeferredProps,
Fetcher,
Hash,
Expand Down
3 changes: 1 addition & 2 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
NavigationContext,
RouteContext,
} from "./lib/context";
import type { Deferrable, NavigateFunction } from "./lib/hooks";
import type { NavigateFunction } from "./lib/hooks";
import {
useHref,
useInRouterContext,
Expand Down Expand Up @@ -102,7 +102,6 @@ export type {
ActionFunctionArgs,
DataMemoryRouterProps,
DataRouteMatch,
Deferrable,
DeferredProps,
Fetcher,
Hash,
Expand Down
3 changes: 1 addition & 2 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
DataRouterStateContext,
DeferredContext,
} from "./context";
import type { ResolvedDeferrable } from "./hooks";
import {
useDeferredData,
useInRouterContext,
Expand Down Expand Up @@ -431,7 +430,7 @@ function DataRoutes({
}

export interface DeferredResolveRenderFunction<Data> {
(data: ResolvedDeferrable<Data>): JSX.Element;
(data: Awaited<Data>): JSX.Element;
}

export interface DeferredProps<Data>
Expand Down
11 changes: 1 addition & 10 deletions packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -724,21 +724,12 @@ export function useRouteError() {
return state.errors?.[thisRoute.route.id];
}

export type Deferrable<T> = never | T | Promise<T>;
export type ResolvedDeferrable<T> = T extends null | undefined
? T
: T extends Deferrable<infer T2>
? T2 extends Promise<infer T3>
? T3
: T2
: T;

/**
* Returns the happy-path data from the nearest ancestor <Deferred /> value
*/
export function useDeferredData<Data>() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacob-ebey @pcattori We decided to leave generics off useLoaderData/useActionData for 6.4.0 so we can see how they differ in reality when you remove the network boundary, and we figure we will introduce the proper generics for them in a 6.5.0+ release. Should we do the same for useDeferredData? Or do we think this type is safe and won't necessarily need to "break" based on what we land on for useLoaderData.

Mainly want to make sure that the types that work in remix will work here too:

useDeferredData<UseDataFunctionReturn<typeof loader>["deferredProp"]>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure here. I'm tempted to say rip it off until we have RR fully integrated in a SSR setup so we don't have any potential breaking changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'm leaning that way too 🪓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let value = React.useContext(DeferredContext);
return value as ResolvedDeferrable<Data>;
return value as Awaited<Data>;
}

const alreadyWarned: Record<string, boolean> = {};
Expand Down