Skip to content

Commit

Permalink
Add TS/JS tabs and update from load() to preload() (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
atilafassina authored Sep 27, 2024
1 parent 40fbe0a commit db0e87d
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions src/routes/solid-start/building-your-application/data-loading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Solid provides a way to load data from your data sources using the [`createResou
It takes an async function and returns a [signal](/reference/basic-reactivity/create-signal) from it.
`createResource` integrates with [`Suspense`](/reference/components/suspense) and [`ErrorBoundary`](/reference/components/error-boundary) to help manage lifecycle and error states.

<TabsCodeBlocks>
<div id="ts">
```tsx {6-9}
import { For, createResource } from "solid-js";

Expand All @@ -26,6 +28,22 @@ export default function Page() {
return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>
<div id="js">
```tsx {4-7}
import { For, createResource } from "solid-js";

export default function Page() {
const [users] = createResource(async () => {
const response = await fetch("https://example.com/users");
return (await response.json());
});

return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>
</TabsCodeBlocks>

When fetching inside components, you can encounter unnecessary waterfalls, especially when nested under lazy loaded sections.
To solve this, it can be valuable to introduce a hoist and cache mechanism.
Expand All @@ -36,6 +54,8 @@ for the example below we will be using the data in APIs in [`solid-router`](/sol

Using some of the features of `solid-router`, we can create a cache for our data:

<TabsCodeBlocks>
<div id="ts">
```tsx title="/routes/users.tsx" {6, 9, 12}
import { For } from "solid-js";
import { createAsync, cache } from "@solidjs/router";
Expand All @@ -48,7 +68,7 @@ const getUsers = cache(async () => {
}, "users");

export const route = {
load: () => getUsers(),
preload: () => getUsers(),
};

export default function Page() {
Expand All @@ -57,6 +77,29 @@ export default function Page() {
return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>
<div id="js">
```tsx title="/routes/users.jsx" {4, 7, 10}
import { For } from "solid-js";
import { createAsync, cache } from "@solidjs/router";

const getUsers = cache(async () => {
const response = await fetch("https://example.com/users");
return (await response.json());
}, "users");

export const route = {
preload: () => getUsers(),
};

export default function Page() {
const users = createAsync(() => getUsers());

return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>
</TabsCodeBlocks>

With this method, however, there are some caveats to be aware of:

Expand All @@ -78,6 +121,8 @@ Through the `"use server"` comment you can tell the bundler to create an RPC and
This lets you write code that only runs on the server without needing to create an API route for it.
For example, it could be database access or internal APIs, or when you sit within your function and need to use your server.

<TabsCodeBlocks>
<div id="ts">
```tsx title="/routes/users.tsx" {7}
import { For } from "solid-js";
import { createAsync, cache } from "@solidjs/router";
Expand All @@ -90,7 +135,30 @@ const getUsers = cache(async () => {
}, "users");

export const route = {
load: () => getUsers(),
preload: () => getUsers(),
};

export default function Page() {
const users = createAsync(() => getUsers());

return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>


<div id="js">
```tsx title="/routes/users.jsx" {5}
import { For } from "solid-js";
import { createAsync, cache } from "@solidjs/router";

const getUsers = cache(async () => {
"use server";
return store.users.list();
}, "users");

export const route = {
preload: () => getUsers(),
};

export default function Page() {
Expand All @@ -99,3 +167,5 @@ export default function Page() {
return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
}
```
</div>
</TabsCodeBlocks>

0 comments on commit db0e87d

Please sign in to comment.