docs: make 2.x the default version (#3369)
This commit is contained in:
@@ -26,16 +26,16 @@ template which can be conditioned based on state and params. Note that any state
|
||||
set by middleware is available via `props.state`.
|
||||
|
||||
```tsx routes/sub/_layout.tsx
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
import { define } from "../../utils.ts";
|
||||
|
||||
export default function Layout({ Component, state }: PageProps) {
|
||||
export default define.layout({ Component, state }) => {
|
||||
// do something with state here
|
||||
return (
|
||||
<div class="layout">
|
||||
<Component />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Async layouts
|
||||
@@ -44,33 +44,12 @@ In case you need to fetch data asynchronously before rendering the layout, you
|
||||
can use an async layout to do so.
|
||||
|
||||
```tsx routes/sub/_layout.tsx
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
import { define } from "../../utils.ts";
|
||||
|
||||
export default async function Layout(req: Request, ctx: FreshContext) {
|
||||
export default define.layout(async (ctx) => {
|
||||
// do something with state here
|
||||
const data = await loadData();
|
||||
|
||||
return (
|
||||
<div class="layout">
|
||||
<p>{data.greeting}</p>
|
||||
<ctx.Component />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### Define helper
|
||||
|
||||
To make it a little quicker to write async layouts, Fresh ships with a
|
||||
`defineLayout` helper which automatically infers the correct types for the
|
||||
function arguments.
|
||||
|
||||
```tsx routes/greet/_layout.tsx
|
||||
import { defineLayout } from "$fresh/server.ts";
|
||||
|
||||
export default defineLayout(async (req, ctx) => {
|
||||
const data = await loadData();
|
||||
|
||||
return (
|
||||
<div class="layout">
|
||||
<p>{data.greeting}</p>
|
||||
@@ -99,21 +78,22 @@ To make `routes/sub/special.tsx` opt out of rendering layouts we can set
|
||||
`skipInheritedLayouts: true`.
|
||||
|
||||
```tsx routes/sub/special.tsx
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
import { type RouteConfig } from "fresh";
|
||||
import { define } from "../../utils.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
skipInheritedLayouts: true, // Skip already inherited layouts
|
||||
};
|
||||
|
||||
export default function MyPage() {
|
||||
export default define.layout(() => {
|
||||
return <p>Hello world</p>;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
You can skip already inherited layouts inside a layout file:
|
||||
|
||||
```tsx routes/special/_layout.tsx
|
||||
import { LayoutConfig } from "$fresh/server.ts";
|
||||
import { type LayoutConfig } from "fresh";
|
||||
|
||||
export const config: LayoutConfig = {
|
||||
skipInheritedLayouts: true, // Skip already inherited layouts
|
||||
|
||||
Reference in New Issue
Block a user