-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
566672b
commit 36975ff
Showing
15 changed files
with
471 additions
and
106 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,7 @@ | ||
--- | ||
"react-router": minor | ||
"react-router-dom": minor | ||
"@remix-run/router": minor | ||
--- | ||
|
||
Add support for `route.lazy` for code-splitting and lazy-loading of route modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from "react"; | ||
import type { | ||
ActionFunction, | ||
ShouldRevalidateFunction, | ||
} from "react-router-dom"; | ||
import { Form, useLoaderData } from "react-router-dom"; | ||
|
||
interface LazyLoaderData { | ||
date: string; | ||
submissionCount: number; | ||
} | ||
|
||
let submissionCount = 0; | ||
|
||
export const loader = async (): Promise<LazyLoaderData> => { | ||
console.log("lazy loader start"); | ||
await new Promise((r) => setTimeout(r, 1000)); | ||
console.log("lazy loader end"); | ||
return { | ||
date: new Date().toISOString(), | ||
submissionCount, | ||
}; | ||
}; | ||
|
||
export const action: ActionFunction = async ({ request }) => { | ||
console.log("lazy action start"); | ||
await new Promise((r) => setTimeout(r, 1000)); | ||
console.log("lazy action end"); | ||
|
||
let body = await request.formData(); | ||
if (body.get("error")) { | ||
throw new Error("Form action error"); | ||
} | ||
|
||
submissionCount++; | ||
return submissionCount; | ||
}; | ||
|
||
export function ErrorBoundary() { | ||
return ( | ||
<> | ||
<h2>Lazy error boundary</h2> | ||
<pre>Something went wrong</pre> | ||
</> | ||
); | ||
} | ||
|
||
export const shouldRevalidate: ShouldRevalidateFunction = (args) => { | ||
return Boolean(args.formAction); | ||
}; | ||
|
||
export default function LazyPage() { | ||
let data = useLoaderData() as LazyLoaderData; | ||
|
||
return ( | ||
<> | ||
<h2>Lazy</h2> | ||
<p>Date from loader: {data.date}</p> | ||
<p>Form submission count: {data.submissionCount}</p> | ||
<Form method="post"> | ||
<div style={{ display: "flex", gap: 12 }}> | ||
<button>Submit form</button> | ||
<button name="error" value="true"> | ||
Throw an error | ||
</button> | ||
</div> | ||
</Form> | ||
</> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.