-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove useLoaderData from errorElement (#2152)
* remove useLoaderData from errorElement and refactor root to use Remix's Layout export * add changeset * fix merge missed
- Loading branch information
Showing
28 changed files
with
1,025 additions
and
1,162 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
'skeleton': patch | ||
'@shopify/cli-hydrogen': patch | ||
--- | ||
|
||
Refactor root to use [Remix's Layout Export pattern](https://remix.run/docs/en/main/file-conventions/root#layout-export). | ||
This will also fix below error ahead of Single Fetch future flag. | ||
|
||
> You cannot `useLoaderData` in an errorElement | ||
The diff below showcase how you can make this refactor in your existing application. | ||
|
||
```diff | ||
import { | ||
Outlet, | ||
- useLoaderData, | ||
+ useRouteLoaderData, | ||
} from '@remix-run/react'; | ||
-import {Layout} from '~/components/Layout'; | ||
+import {PageLayout} from '~/components/PageLayout'; | ||
|
||
-export default function App() { | ||
+export function Layout({children}: {children?: React.ReactNode}) { | ||
const nonce = useNonce(); | ||
- const data = useLoaderData<typeof loader>(); | ||
+ const data = useRouteLoaderData<typeof loader>('root'); | ||
|
||
return ( | ||
<html> | ||
... | ||
<body> | ||
- <Layout {...data}> | ||
- <Outlet /> | ||
- </Layout> | ||
+ {data? ( | ||
+ <PageLayout {...data}> | ||
+ {children} | ||
+ </PageLayout> | ||
+ ) : ( | ||
+ children | ||
+ )} | ||
</body> | ||
</html> | ||
); | ||
} | ||
|
||
+export default function App() { | ||
+ return <Outlet />; | ||
+} | ||
|
||
export function ErrorBoundary() { | ||
const rootData = useLoaderData<typeof loader>(); | ||
|
||
return ( | ||
- <html> | ||
- ... | ||
- <body> | ||
- <Layout {...rootData}> | ||
- <div className="route-error"> | ||
- <h1>Error</h1> | ||
- ... | ||
- </div> | ||
- </Layout> | ||
- </body> | ||
- </html> | ||
+ <div className="route-error"> | ||
+ <h1>Error</h1> | ||
+ ... | ||
+ </div> | ||
); | ||
} | ||
|
||
``` |
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
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
Oops, something went wrong.