You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
thrownewError("Cannot fetch User. Error: "+ (err as Error).message);
261
+
}
262
+
};
263
+
constUsers= () => {
264
+
constpromise=createPromise(getUser);
265
+
266
+
return (
267
+
<div>
268
+
{() => {
269
+
if (promise.value.data) {
270
+
return<div>{promise.value.data}</div>;
271
+
}
272
+
if (promise.value.error) {
273
+
return<div>{promise.value.error.message}</div>;
274
+
}
275
+
return<div>Loading...</div>;
276
+
}}
277
+
</div>
278
+
);
279
+
};
280
+
281
+
exportdefaultUsers;
282
+
```
283
+
284
+
### Lazy Loading Components
285
+
286
+
You can use the built-in `lazy` function to load components only when they are needed. This improves initial load times and enables a smoother user experience by displaying a pending UI while the component loads.
287
+
288
+
> Note: Lazy loading only works for loading functional components.
289
+
> Important Points:
290
+
291
+
- The `lazy` function returns another functional component that wraps your imported component.
292
+
- The new component has two extra props `errorFallback` and `fallback`.
293
+
-`fallback` takes another component or jsx as a fallback UI while the component loads.
294
+
-`errorFallback` takes a fallback component or a function with the error as argument ie.,
Refract doesn’t come with a built-in way to create context or global state—but who needs that when you can just toss your signals into a separate file and import them wherever you want? It works exactly as expected. It’s just JavaScript, so why overcomplicate things? 🤷♂️
322
+
115
323
## API Reference
116
324
117
325
### `createElement(type, props, ...children)`
118
326
119
-
Creates a virtual DOM element.
327
+
Creates a virtual DOM element. This function is used internally by JSX to convert JSX syntax into virtual DOM representations.
120
328
121
329
### `render(element, container)`
122
330
123
-
Renders a JSX component to the DOM.
331
+
Renders a JSX component to the DOM. It takes a JSX element and a container DOM node as parameters.
332
+
333
+
### `createSignal(initialValue)`
334
+
335
+
Creates a signal for state management.
336
+
337
+
-**Properties:**
338
+
-`value`: A read-only reactive value.
339
+
-**Methods:**
340
+
-`update(newValue | updater)`: Updates the signal's value.
341
+
342
+
### `computed(computation)`
343
+
344
+
Creates a computed value that automatically updates when its dependent signals change.
345
+
346
+
-**Parameter:**
347
+
-`computation`: A function that returns a computed value based on one or more signals.
348
+
349
+
### `createEffect(effect)`
350
+
351
+
Creates an effect that runs after the component has mounted and whenever its dependent signals update.
352
+
353
+
-**Parameter:**
354
+
-`effect`: A function that can optionally return a cleanup function.
355
+
356
+
### `cleanUp(cleanupFn)`
357
+
358
+
Registers a cleanup function to be executed when the component unmounts.
359
+
360
+
-**Parameter:**
361
+
-`cleanupFn`: A function that performs cleanup operations.
362
+
363
+
### `createRef()`
364
+
365
+
Creates a reference object for DOM elements.
366
+
367
+
-**Property:**
368
+
-`current`: Initially `null`, then set to the DOM element once rendered.
369
+
370
+
### `createPromise(asyncFunction)`
371
+
372
+
Wraps an asynchronous function into a promise handler that tracks its state.
373
+
374
+
-**Returns:**
375
+
- A read-only signal with properties:
376
+
-`data`: The resolved data or `null`.
377
+
-`error`: The error object or `null`.
378
+
-`status`: `"pending"`, `"resolved"`, or `"rejected"`.
379
+
380
+
### `lazy(loader)`
124
381
125
-
### `useState(initialValue)`
382
+
Lazily loads a functional component.
126
383
127
-
A simple state management hook.
384
+
-**Parameter:**
385
+
-`loader`: A function that returns a promise resolving to a module with a default export (the component).
386
+
-**Additional Props on the Lazy Component:**
387
+
-`fallback`: JSX to render while loading.
388
+
-`errorFallback`: A fallback UI or function to render if an error occurs.
0 commit comments