1111 - loading
1212 - error-handling
1313 - ssr
14- version : ' 1.0'
14+ version : " 1.0"
1515description : >-
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
3333function 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
3939function 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
5151type 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
5959type 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
6671type 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
7176interface 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.
8896When the source changes, the fetcher is automatically re-run.
8997
9098### ` fetcher `
99+
91100- ** Type:** ` ResourceFetcher<S, T, R> `
92101
93102Function 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
99109Configuration 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.
106117When provided, the resource starts in "ready" state and the type excludes ` undefined ` .
107118
108119#### ` name `
120+
109121- ** Type:** ` string `
110122- ** Default:** ` undefined `
111123
112124A name for debugging purposes in development mode.
113125
114126#### ` deferStream `
127+
115128- ** Type:** ` boolean `
116129- ** Default:** ` false `
117130
118131Controls 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
133148Custom 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
145161Returns a tuple containing the resource accessor and resource actions.
146162
147- ### ` Resource `
163+ ### ` Resource `
148164
149165``` typescript
150166type 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
177192type 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
193208const [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"
207222const [userId, setUserId] = createSignal (1 );
208223
209224const [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);
224239await 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
254268console .log (user ().name ); // "Loading..." initially
@@ -260,7 +274,7 @@ console.log(user().name); // "Loading..." initially
260274const [isEnabled, setIsEnabled] = createSignal (false );
261275
262276const [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```
0 commit comments