-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
c1f907a
commit f3e4289
Showing
14 changed files
with
371 additions
and
51 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
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,63 @@ | ||
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> => { | ||
return { | ||
date: new Date().toISOString(), | ||
submissionCount, | ||
}; | ||
}; | ||
|
||
export const action: ActionFunction = async ({ request }) => { | ||
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
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,63 @@ | ||
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> => { | ||
return { | ||
date: new Date().toISOString(), | ||
submissionCount, | ||
}; | ||
}; | ||
|
||
export const action: ActionFunction = async ({ request }) => { | ||
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> | ||
</> | ||
); | ||
} |
Oops, something went wrong.