Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lint errors
  • Loading branch information
pcattori committed Aug 28, 2024
commit 7c935a95d1fe17129f88e8ebedf93ebc716f8e18
30 changes: 21 additions & 9 deletions docs/guides/single-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ export function loader() {
date: new Date(),
};
}
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date }

export default function Component() {
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date }
}
```

#### Functions and class instances
Expand All @@ -186,8 +189,11 @@ export function loader() {
notSoRandom: () => 7,
};
}
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date, notSoRandom: undefined }

export default function Component() {
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date, notSoRandom: undefined }
}
```

Methods are also not serializable, so class instances get slimmed down to just their serializable properties:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pcattori Couldn't Remix call classInstance.toJSON()? If it's not defined then grab the serializable properties automatically

Copy link
Contributor Author

@pcattori pcattori Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could but that's what json(...) is for. The mental model for Single Fetch is that types are preserved as much as possible. We don't want a class with a bunch of serializable properties (Date, bigint, RegExp, etc.) to show up as a bag of strings on the client.

Expand Down Expand Up @@ -216,8 +222,11 @@ export function loader() {
spot: new Dog("Spot", 3),
};
}
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date, spot: { name: string, age: number, bark: undefined } }

export default function Component() {
const data = useLoaderData<typeof loader>();
// ^? { planet: string, date: Date, spot: { name: string, age: number, bark: undefined } }
}
```

#### `clientLoader` and `clientAction`
Expand All @@ -237,16 +246,19 @@ class Dog {
}

// Make sure to annotate the types for the args! 👇
export function clientLoader({}: ClientLoaderFunctionArgs) {
export function clientLoader(_: ClientLoaderFunctionArgs) {
return {
planet: "world",
date: new Date(),
notSoRandom: () => 7,
spot: new Dog("Spot", 3),
};
}
const data = useLoaderData<typeof clientLoader>();
// ^? { planet: string, date: Date, notSoRandom: () => number, spot: Dog }

export default function Component() {
const data = useLoaderData<typeof clientLoader>();
// ^? { planet: string, date: Date, notSoRandom: () => number, spot: Dog }
}
```

### Headers
Expand Down
Loading