Skip to content

Commit 66afe9b

Browse files
committed
format
1 parent 077cfcd commit 66afe9b

3 files changed

Lines changed: 97 additions & 85 deletions

File tree

src/routes/reference/basic-reactivity/create-resource.mdx

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tags:
1111
- loading
1212
- error-handling
1313
- ssr
14-
version: '1.0'
14+
version: "1.0"
1515
description: >-
1616
Fetch async data with createResource. Handles loading states, errors, and
1717
integrates with Suspense for seamless data fetching in Solid.js applications.
@@ -31,93 +31,107 @@ import { createResource } from "solid-js";
3131
```typescript
3232
// Without source
3333
function createResource<T, R = unknown>(
34-
fetcher: ResourceFetcher<true, T, R>,
35-
options?: ResourceOptions<T>
36-
): ResourceReturn<T, R>
34+
fetcher: ResourceFetcher<true, T, R>,
35+
options?: ResourceOptions<T>
36+
): ResourceReturn<T, R>;
3737

3838
// With source
3939
function createResource<T, S, R = unknown>(
40-
source: ResourceSource<S>,
41-
fetcher: ResourceFetcher<S, T, R>,
42-
options?: ResourceOptions<T, S>
43-
): ResourceReturn<T, R>
40+
source: ResourceSource<S>,
41+
fetcher: ResourceFetcher<S, T, R>,
42+
options?: ResourceOptions<T, S>
43+
): ResourceReturn<T, R>;
4444
```
4545

4646
### Related types
4747

4848
```typescript
49-
type ResourceReturn<T, R = unknown> = [Resource<T>, ResourceActions<T, R>]
49+
type ResourceReturn<T, R = unknown> = [Resource<T>, ResourceActions<T, R>];
5050

5151
type Resource<T> = {
52-
(): T | undefined
53-
state: "unresolved" | "pending" | "ready" | "refreshing" | "errored"
54-
loading: boolean
55-
error: any
56-
latest: T | undefined
57-
}
52+
(): T | undefined;
53+
state: "unresolved" | "pending" | "ready" | "refreshing" | "errored";
54+
loading: boolean;
55+
error: any;
56+
latest: T | undefined;
57+
};
5858

5959
type ResourceActions<T, R = unknown> = {
60-
mutate: (value: T | undefined) => T | undefined
61-
refetch: (info?: R) => Promise<T> | T | undefined
62-
}
60+
mutate: (value: T | undefined) => T | undefined;
61+
refetch: (info?: R) => Promise<T> | T | undefined;
62+
};
6363

64-
type ResourceSource<S> = S | false | null | undefined | (() => S | false | null | undefined)
64+
type ResourceSource<S> =
65+
| S
66+
| false
67+
| null
68+
| undefined
69+
| (() => S | false | null | undefined);
6570

6671
type ResourceFetcher<S, T, R = unknown> = (
67-
source: S,
68-
info: { value: T | undefined; refetching: R | boolean }
69-
) => T | Promise<T>
72+
source: S,
73+
info: { value: T | undefined; refetching: R | boolean }
74+
) => T | Promise<T>;
7075

7176
interface ResourceOptions<T, S = unknown> {
72-
initialValue?: T
73-
name?: string
74-
deferStream?: boolean
75-
ssrLoadFrom?: "initial" | "server"
76-
storage?: (init: T | undefined) => [Accessor<T | undefined>, Setter<T | undefined>]
77-
onHydrated?: (k: S | undefined, info: { value: T | undefined }) => void
77+
initialValue?: T;
78+
name?: string;
79+
deferStream?: boolean;
80+
ssrLoadFrom?: "initial" | "server";
81+
storage?: (
82+
init: T | undefined
83+
) => [Accessor<T | undefined>, Setter<T | undefined>];
84+
onHydrated?: (k: S | undefined, info: { value: T | undefined }) => void;
7885
}
7986
```
8087

8188
## Parameters
8289

8390
### `source`
91+
8492
- **Type:** `ResourceSource<S>`
8593
- **Default:** `undefined`
8694

87-
Reactive data source whose non-nullish and non-false values are passed to the fetcher.
95+
Reactive data source whose non-nullish and non-false values are passed to the fetcher.
8896
When the source changes, the fetcher is automatically re-run.
8997

9098
### `fetcher`
99+
91100
- **Type:** `ResourceFetcher<S, T, R>`
92101

93102
Function that receives the source value (or `true` if no source), the current resource info, and returns a value or Promise.
94103

95104
### `options`
105+
96106
- **Type:** `ResourceOptions<T, S>`
97107
- **Default:** `{}`
98108

99109
Configuration options for the resource.
100110

101111
#### `initialValue`
112+
102113
- **Type:** `T`
103114
- **Default:** `undefined`
104115

105-
Initial value for the resource.
116+
Initial value for the resource.
106117
When provided, the resource starts in "ready" state and the type excludes `undefined`.
107118

108119
#### `name`
120+
109121
- **Type:** `string`
110122
- **Default:** `undefined`
111123

112124
A name for debugging purposes in development mode.
113125

114126
#### `deferStream`
127+
115128
- **Type:** `boolean`
116129
- **Default:** `false`
117130

118131
Controls streaming behavior during server-side rendering.
119132

120133
#### `ssrLoadFrom`
134+
121135
- **Type:** `"initial" | "server"`
122136
- **Default:** `"server"`
123137

@@ -127,12 +141,14 @@ Determines how the resource loads during SSR hydration.
127141
- "initial": Re-fetches on the client after hydration.
128142

129143
#### `storage`
144+
130145
- **Type:** `(init: T | undefined) => [Accessor<T | undefined>, Setter<T | undefined>]`
131146
- **Default:** `createSignal`
132147

133148
Custom storage function for the resource value, useful for persistence or custom state management.
134149

135150
#### `onHydrated`
151+
136152
- **Type:** `(k: S | undefined, info: { value: T | undefined }) => void`
137153
- **Default:** `undefined`
138154

@@ -144,55 +160,54 @@ Callback fired when the resource hydrates on the client side.
144160

145161
Returns a tuple containing the resource accessor and resource actions.
146162

147-
### `Resource`
163+
### `Resource`
148164

149165
```typescript
150166
type Resource<T> = {
151-
(): T | undefined
152-
state: "unresolved" | "pending" | "ready" | "refreshing" | "errored"
153-
loading: boolean
154-
error: any
155-
latest: T | undefined
156-
}
167+
(): T | undefined;
168+
state: "unresolved" | "pending" | "ready" | "refreshing" | "errored";
169+
loading: boolean;
170+
error: any;
171+
latest: T | undefined;
172+
};
157173
```
158174

159175
- `state`: Current state of the resource.
160-
See table below for state descriptions.
176+
See table below for state descriptions.
161177
- `loading`: Indicates if the resource is currently loading.
162178
- `error`: Error information if the resource failed to load.
163179
- `latest`: The latest value of the resource.
164180

165-
| State | Description | Loading | Error | Latest |
166-
| ------ | ----------- | ------- | ----- | ------ |
167-
| `unresolved` | Initial state, not yet fetched | `false` | `undefined` | `undefined` |
168-
| `pending` | Fetching in progress | `true` | `undefined` | `undefined` |
169-
| `ready` | Successfully fetched | `false` | `undefined` | `T` |
170-
| `refreshing` | Refetching while keeping previous value | `true` | `undefined` | `T` |
171-
| `errored` | Fetching failed | `false` | `any` | `undefined` |
172-
181+
| State | Description | Loading | Error | Latest |
182+
| ------------ | --------------------------------------- | ------- | ----------- | ----------- |
183+
| `unresolved` | Initial state, not yet fetched | `false` | `undefined` | `undefined` |
184+
| `pending` | Fetching in progress | `true` | `undefined` | `undefined` |
185+
| `ready` | Successfully fetched | `false` | `undefined` | `T` |
186+
| `refreshing` | Refetching while keeping previous value | `true` | `undefined` | `T` |
187+
| `errored` | Fetching failed | `false` | `any` | `undefined` |
173188

174189
### `ResourceActions`
175190

176191
```typescript
177192
type ResourceActions<T, R = unknown> = {
178-
mutate: (value: T | undefined) => T | undefined
179-
refetch: (info?: R) => Promise<T> | T | undefined
180-
}
193+
mutate: (value: T | undefined) => T | undefined;
194+
refetch: (info?: R) => Promise<T> | T | undefined;
195+
};
181196
```
182197

183198
- `mutate`: Function to Manually overwrite the resource value without calling the fetcher.
184-
Allows you to optimistically update the resource value locally, without making a network request.
199+
Allows you to optimistically update the resource value locally, without making a network request.
185200
- `refetch`: Function to re-run the fetcher without changing the source.
186-
If a parameter is provided to `refetch`, it will be passed to the fetcher's `refetching` property.
201+
If a parameter is provided to `refetch`, it will be passed to the fetcher's `refetching` property.
187202

188203
## Examples
189204

190205
### Basic usage
191206

192207
```typescript
193208
const [data] = createResource(async () => {
194-
const response = await fetch('/api/data');
195-
return response.json();
209+
const response = await fetch("/api/data");
210+
return response.json();
196211
});
197212

198213
// Access data
@@ -207,8 +222,8 @@ console.log(data.state); // "pending" → "ready"
207222
const [userId, setUserId] = createSignal(1);
208223

209224
const [user] = createResource(userId, async (id) => {
210-
const response = await fetch(`/api/users/${id}`);
211-
return response.json();
225+
const response = await fetch(`/api/users/${id}`);
226+
return response.json();
212227
});
213228

214229
// Automatically refetches when userId changes
@@ -224,7 +239,7 @@ const [posts, { refetch, mutate }] = createResource(fetchPosts);
224239
await refetch();
225240

226241
// Optimistic update
227-
mutate(posts => [...posts, newPost]);
242+
mutate((posts) => [...posts, newPost]);
228243
```
229244

230245
### Error handling
@@ -245,10 +260,9 @@ const [data] = createResource(async () => {
245260
### With Initial Value
246261

247262
```typescript
248-
const [user] = createResource(
249-
() => fetchUser(),
250-
{ initialValue: { name: 'Loading...', id: 0 } }
251-
);
263+
const [user] = createResource(() => fetchUser(), {
264+
initialValue: { name: "Loading...", id: 0 },
265+
});
252266

253267
// user() is never undefined
254268
console.log(user().name); // "Loading..." initially
@@ -260,7 +274,7 @@ console.log(user().name); // "Loading..." initially
260274
const [isEnabled, setIsEnabled] = createSignal(false);
261275

262276
const [data] = createResource(
263-
() => isEnabled() && userId(), // Only fetches when enabled and userId exists
264-
async (id) => fetchUserData(id)
277+
() => isEnabled() && userId(), // Only fetches when enabled and userId exists
278+
async (id) => fetchUserData(id)
265279
);
266280
```

src/routes/reference/basic-reactivity/create-signal.mdx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tags:
99
- reactivity
1010
- core
1111
- primitives
12-
version: '1.0'
12+
version: "1.0"
1313
description: >-
1414
Create reactive state with createSignal, Solid's fundamental primitive. Track
1515
values that change over time and automatically update your UI when they do.
@@ -27,47 +27,47 @@ import { createSignal } from "solid-js";
2727
## Type Signature
2828

2929
```typescript
30-
function createSignal<T>(): Signal<T | undefined>
31-
function createSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>
30+
function createSignal<T>(): Signal<T | undefined>;
31+
function createSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
3232
```
3333

3434
### Related Types
3535

3636
```typescript
37-
type Signal<T> = [get: Accessor<T>, set: Setter<T>]
37+
type Signal<T> = [get: Accessor<T>, set: Setter<T>];
3838

39-
type Accessor<T> = () => T
39+
type Accessor<T> = () => T;
4040

4141
type Setter<T> = {
42-
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
43-
<U extends T>(value: (prev: T) => U): U;
44-
<U extends T>(value: Exclude<U, Function>): U;
45-
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
46-
}
42+
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
43+
<U extends T>(value: (prev: T) => U): U;
44+
<U extends T>(value: Exclude<U, Function>): U;
45+
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
46+
};
4747

4848
interface SignalOptions<T> {
49-
name?: string;
50-
equals?: false | ((prev: T, next: T) => boolean);
51-
internal?: boolean;
49+
name?: string;
50+
equals?: false | ((prev: T, next: T) => boolean);
51+
internal?: boolean;
5252
}
5353
```
5454

5555
## Parameters
5656

5757
### `value`
5858

59-
- **Type:** `T`
59+
- **Type:** `T`
6060
- **Default:** `undefined`
6161

6262
The initial value for the signal. If no initial value is provided, the signal's type is automatically extended with `undefined`.
6363

64-
### `options`
64+
### `options`
6565

66-
- **Type:** `SignalOptions<T>`
66+
- **Type:** `SignalOptions<T>`
6767
- **Default:** `undefined`
6868

6969
Configuration object for the signal.
70-
In production mode, all debugging metadata is stripped away for optimal performance.
70+
In production mode, all debugging metadata is stripped away for optimal performance.
7171
Dev-only options like `name` are ignored, and no warnings are issued, ensuring that your application runs as efficiently as possible.
7272

7373
#### `name`
@@ -84,17 +84,16 @@ The name will show up in console messages and in the [Solid devtools](https://gi
8484
- **Default:** `false`
8585

8686
By default, signals use reference equality (`===`).
87-
A custom comparison function to determine when the signal should update.
87+
A custom comparison function to determine when the signal should update.
8888
If set to `false`, the signal will always update regardless of value equality.
8989
This can be useful to create a Signal that triggers manual updates in the reactive system, but must remain a pure function.
9090

91-
9291
#### `internal`
9392

9493
- **Type:** `boolean`
9594
- **Default:** `false`
9695

97-
Marks the signal as internal, preventing it from appearing in development tools.
96+
Marks the signal as internal, preventing it from appearing in development tools.
9897
This is primarily used by Solid's internal systems.
9998

10099
## Return Value
@@ -123,5 +122,4 @@ function Counter() {
123122
</div>
124123
);
125124
}
126-
127-
```
125+
```

src/routes/reference/secondary-primitives/create-computed.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ function UserEditor(props: UserEditorProps) {
125125
## Related
126126

127127
- [`createMemo`](/reference/basic-reactivity/create-memo)
128-
- [`createRenderEffect`](/reference/secondary-primitives/create-render-effect)
128+
- [`createRenderEffect`](/reference/secondary-primitives/create-render-effect)

0 commit comments

Comments
 (0)