-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Shopify/@juanpprieto/refactor-hooks
Add useDeferred and improve useCart and useCountries
- Loading branch information
Showing
4 changed files
with
36 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import {useParentRouteData} from './useRouteData'; | ||
|
||
import {useMatches} from '@remix-run/react'; | ||
import {useDeferred} from './useDeferred'; | ||
import type {Cart} from '@shopify/hydrogen-ui-alpha/storefront-api-types'; | ||
|
||
/* | ||
This is an experimental pattern that helps prevent props drilling | ||
*/ | ||
export function useCart(): Cart | null { | ||
const rootData = useParentRouteData('/'); | ||
|
||
if (typeof rootData?.cart === 'undefined') { | ||
return null; | ||
} | ||
|
||
if (rootData?.cart?._data) { | ||
return rootData?.cart?._data; | ||
} | ||
|
||
throw rootData?.cart; | ||
const [root] = useMatches(); | ||
return useDeferred('cart', root); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import {useParentRouteData} from './useRouteData'; | ||
|
||
import {useMatches} from '@remix-run/react'; | ||
import {useDeferred} from './useDeferred'; | ||
import type {Country} from '@shopify/hydrogen-ui-alpha/storefront-api-types'; | ||
|
||
/* | ||
This is an experimental pattern that helps prevent props drilling | ||
*/ | ||
export function useCountries(): Array<Country> | null { | ||
const rootData = useParentRouteData('/'); | ||
|
||
if (typeof rootData?.countries === 'undefined') { | ||
return null; | ||
} | ||
|
||
if (rootData?.countries?._data) { | ||
return rootData?.countries?._data; | ||
} | ||
|
||
throw rootData?.countries; | ||
const [root] = useMatches(); | ||
return useDeferred('countries', root); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {RouteMatch} from '@remix-run/react'; | ||
|
||
/* | ||
A utility hook to access defer props from a given route | ||
*/ | ||
export function useDeferred(resource: string, route: RouteMatch) { | ||
if (!route) { | ||
throw new Error('route not provided'); | ||
} | ||
if (!resource) { | ||
throw new Error('resource not provided'); | ||
} | ||
const isPromise = Boolean(route?.data?.[resource]?.then); | ||
|
||
if (isPromise) { | ||
// the [resource] promise resolved, returned promised data | ||
if (route?.data?.[resource]?._data) { | ||
return route?.data?.[resource]?._data; | ||
} | ||
|
||
// Promise not yet resolved, throw while data is ready | ||
// Must be caught by a wrapping suspense boundary | ||
throw route?.data?.[resource]; | ||
} | ||
|
||
// the [resource] was awaited in the loader return it | ||
return route.data[resource]; | ||
} |
This file was deleted.
Oops, something went wrong.