Skip to content

Commit 4461182

Browse files
committed
Update the structure of props and options reference
1 parent dd580f9 commit 4461182

32 files changed

+771
-235
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: createMemo
33
---
44

5-
Memos let you efficiently use a derived value in many reactive computations.
5+
Memos let you efficiently use a derived value in many reactive computations.
66
`createMemo` creates a readonly reactive value equal to the return value of the given function and makes sure that function only gets executed when its dependencies change.
77

88
```tsx
@@ -25,9 +25,9 @@ const value = createMemo(() => computeExpensiveValue(a(), b()))
2525
value()
2626
```
2727

28-
In Solid, you often don't need to wrap functions in memos; you can alternatively just define and call a regular function to get similar reactive behavior.
29-
The main difference is when you call the function in multiple reactive settings.
30-
In this case, when the function's dependencies update, the function will get called multiple times unless it is wrapped in createMemo.
28+
In Solid, you often don't need to wrap functions in memos; you can alternatively just define and call a regular function to get similar reactive behavior.
29+
The main difference is when you call the function in multiple reactive settings.
30+
In this case, when the function's dependencies update, the function will get called multiple times unless it is wrapped in createMemo.
3131
For example:
3232

3333
```tsx
@@ -43,30 +43,42 @@ return (
4343
)
4444
```
4545

46-
When the username signal updates, searchForUser will get called just once.
46+
When the username signal updates, searchForUser will get called just once.
4747
If the returned user actually changed, the user memo updates, and then both list items will update automatically.
4848

4949
If we had instead defined user as a plain function `() => searchForUser(username())`, then `searchForUser` would have been called twice, once when updating each list item.
5050

51-
Another key difference is that a memo can shield dependents from updating when the memo's dependencies change but the resulting memo value doesn't.
52-
Like [createSignal](/reference/basic-reactivity/create-signal), the derived signal made by `createMemo` updates (and triggers dependents to rerun) only when the value returned by the memo function actually changes from the previous value, according to JavaScript's `===` operator.
51+
Another key difference is that a memo can shield dependents from updating when the memo's dependencies change but the resulting memo value doesn't.
52+
Like [createSignal](/reference/basic-reactivity/create-signal), the derived signal made by `createMemo` updates (and triggers dependents to rerun) only when the value returned by the memo function actually changes from the previous value, according to JavaScript's `===` operator.
5353
Alternatively, you can pass an options object with `equals` set to false to always update the memo when its dependencies change, or you can pass your own `equals` function for testing equality.
5454

55-
The memo function is called with an argument equal to the value returned from the previous execution of the memo function, or, on the first call, equal to the optional second argument to `createMemo`.
55+
The memo function is called with an argument equal to the value returned from the previous execution of the memo function, or, on the first call, equal to the optional second argument to `createMemo`.
5656
This is useful for reducing computations, such as:
5757

5858
```tsx
5959
// track the sum of all values taken on by input() as it updates
6060
const sum = createMemo((prev) => input() + prev, 0)
6161
```
6262

63-
The memo function should not change other signals by calling setters (it should be "pure").
63+
The memo function should not change other signals by calling setters (it should be "pure").
6464
This enables Solid to optimize the execution order of memo updates according to their dependency graph, so that all memos can update at most once in response to a dependency change.
6565

66-
## Options and arguments
66+
## Parameters
6767

68-
| Name | Type | Description |
69-
| :------ | :------------------------------------------------------ | :------------------------------------------------------------- |
70-
| fn | `(v: T) => T` | The function to memoize. |
71-
| value | `T` | The initial value of the memo. |
72-
| options | `{ equals?: false \| ((prev: T, next: T) => boolean) }` | An optional object with an `equals` function to test equality. |
68+
### `fn`
69+
70+
**Type**: `(v: T) => T`
71+
72+
The function to memoize.
73+
74+
### `value`
75+
76+
**Type**: `T`
77+
78+
The initial value of the memo.
79+
80+
### `options`
81+
82+
**Type**: `{ equals?: false | ((prev: T, next: T) => boolean) }`
83+
84+
An optional object with an `equals` function to test equality.

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

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: createResource
44

55
`createResource` takes an asynchronous fetcher function and returns a signal that is updated with the resulting data when the fetcher completes.
66

7-
There are two ways to use `createResource`: you can pass the fetcher function as the sole argument, or you can additionally pass a source signal as the first argument.
7+
There are two ways to use `createResource`: you can pass the fetcher function as the sole argument, or you can additionally pass a source signal as the first argument.
88
The source signal will retrigger the fetcher whenever it changes, and its value will be passed to the fetcher.
99

1010
```tsx
@@ -15,15 +15,15 @@ const [data, { mutate, refetch }] = createResource(fetchData)
1515
const [data, { mutate, refetch }] = createResource(source, fetchData)
1616
```
1717

18-
In these snippets, the fetcher is the function `fetchData`, and `data()` is undefined until `fetchData` finishes resolving.
19-
In the first case, `fetchData` will be called immediately.
20-
In the second, `fetchData` will be called as soon as `source` has any value other than false, null, or undefined.
18+
In these snippets, the fetcher is the function `fetchData`, and `data()` is undefined until `fetchData` finishes resolving.
19+
In the first case, `fetchData` will be called immediately.
20+
In the second, `fetchData` will be called as soon as `source` has any value other than false, null, or undefined.
2121
It will be called again whenever the value of `source` changes, and that value will always be passed to `fetchData` as its first argument.
2222

23-
You can call `mutate` to directly update the `data` signal (it works like any other signal setter).
23+
You can call `mutate` to directly update the `data` signal (it works like any other signal setter).
2424
You can also call refetch to rerun the fetcher directly, and pass an optional argument to provide additional info to the fetcher e.g `refetch(info)`.
2525

26-
`data` works like a normal signal getter: use `data()` to read the last returned value of `fetchData`.
26+
`data` works like a normal signal getter: use `data()` to read the last returned value of `fetchData`.
2727
But it also has extra reactive properties:
2828

2929
- `data.loading`: whether the fetcher has been called but not returned.
@@ -33,18 +33,18 @@ But it also has extra reactive properties:
3333
- Fetcher throws an `Error` instance, `data.error` will be that instance.
3434
- If the fetcher throws a string, `data.error.message` will contain that string.
3535
- When the fetcher throws a value that is neither an `Error` nor a string, that value will be available as `data.error.cause`.
36-
37-
- As of **v1.4.0**, `data.latest` returns the last value received and will not trigger [Suspense](/reference/components/suspense) or [transitions](#TODO); if no value has been returned yet, `data.latest` will act the same as `data()`.
36+
37+
- As of **v1.4.0**, `data.latest` returns the last value received and will not trigger [Suspense](/reference/components/suspense) or [transitions](#TODO); if no value has been returned yet, `data.latest` will act the same as `data()`.
3838
This can be useful if you want to show the out-of-date data while the new data is loading.
3939

4040
`loading`, `error`, and `latest` are reactive getters and can be tracked.
4141

4242
## The fetcher
4343

4444
The `fetcher` is the async function that you provide to `createResource` to actually fetch the data.
45-
It is passed two arguments: the value of the source signal (if provided), and an info object with two properties: `value` and `refetching`.
46-
The `value` property tells you the previously fetched value.
47-
The `refetching` property is true if the `fetcher` was triggered using the refetch function and false otherwise.
45+
It is passed two arguments: the value of the source signal (if provided), and an info object with two properties: `value` and `refetching`.
46+
The `value` property tells you the previously fetched value.
47+
The `refetching` property is true if the `fetcher` was triggered using the refetch function and false otherwise.
4848
If the `refetch` function was called with an argument (`refetch(info)`), refetching is set to that argument.
4949

5050
```tsx
@@ -93,7 +93,7 @@ const [user] = createResource(() => params.id, fetchUser, {
9393

9494
#### v1.5.0
9595

96-
1. We've added a new state field which covers a more detailed view of the Resource state beyond `loading` and `error`.
96+
1. We've added a new state field which covers a more detailed view of the Resource state beyond `loading` and `error`.
9797
You can now check whether a Resource is `unresolved`, `pending`, `ready`, `refreshing`, or `errored`.
9898

9999
| State | Value resolved | Loading | Has error |
@@ -105,7 +105,7 @@ You can now check whether a Resource is `unresolved`, `pending`, `ready`, `refre
105105
| `errored` | No | No | Yes |
106106

107107
2. When server-rendering resources, especially when embedding Solid in other systems that fetch data before rendering, you might want to initialize the resource with this prefetched value instead of fetching again and having the resource serialize it in its own state.
108-
You can use the new `ssrLoadFrom` option for this.
108+
You can use the new `ssrLoadFrom` option for this.
109109
Instead of using the default `server` value, you can pass `initial` and the resource will use `initialValue` as if it were the result of the first fetch for both SSR and hydration.
110110

111111
```tsx
@@ -115,7 +115,7 @@ const [data, { mutate, refetch }] = createResource(() => params.id, fetchUser, {
115115
})
116116
```
117117

118-
3. Resources can be set with custom defined storage with the same signature as a Signal by using the storage option.
118+
3. Resources can be set with custom defined storage with the same signature as a Signal by using the storage option.
119119
For example using a custom reconciling store could be done this way:
120120

121121
```tsx
@@ -145,14 +145,50 @@ This option is still experimental and may change in the future.
145145

146146
The `createResource` function takes an optional third argument, an options object. The options are:
147147

148-
| Name | Type | Default | Description |
149-
| ------------ | ----------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
150-
| name | `string` | `undefined` | A name for the resource. This is used for debugging purposes. |
151-
| deferStream | `boolean` | `false` | If true, Solid will wait for the resource to resolve before flushing the stream. |
152-
| initialValue | `any` | `undefined` | The initial value of the resource. |
153-
| onHydrated | `function` | `undefined` | A callback that is called when the resource is hydrated. |
154-
| ssrLoadFrom | `"server" \| "initial"` | `"server"` | The source of the initial value for SSR. If set to `"initial"`, the resource will use the `initialValue` option instead of the value returned by the fetcher. |
155-
| storage | `function` | `createSignal` | A function that returns a signal. This can be used to create a custom storage for the resource. This is still experimental |
148+
### `name`
149+
150+
**Type**: `string`
151+
152+
A name for the resource.
153+
This is used for debugging purposes.
154+
155+
### `deferStream`
156+
157+
**Type**: `boolean`
158+
159+
**Default**: `false`
160+
161+
If true, Solid will wait for the resource to resolve before flushing the stream.
162+
163+
### `initialValue`
164+
165+
**Type**: `any`
166+
167+
The initial value of the resource.
168+
169+
### `onHydrated`
170+
171+
**Type**: `function`
172+
173+
A callback that is called when the resource is hydrated.
174+
175+
### `ssrLoadFrom`
176+
177+
**Type**: `"server" | "initial"`
178+
179+
**Default**: `"server"`
180+
181+
The source of the initial value for SSR.
182+
If set to `"initial"`, the resource will use the `initialValue` option instead of the value returned by the fetcher.
183+
184+
### `storage`
185+
186+
**Type**: `function`
187+
188+
**Default**: `createSignal`
189+
190+
A function that returns a signal.
191+
This can be used to create a custom storage for the resource. This is still experimental
156192

157193
## Note for TypeScript users
158194

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: createSignal
33
---
44

5-
Signals are the most basic reactive primitive.
5+
Signals are the most basic reactive primitive.
66
They track a single value (which can be a value of any type) that changes over time.
77

88
```tsx
@@ -38,8 +38,8 @@ Calling the getter (e.g., `count()` or `ready()`) returns the current value of t
3838

3939
Crucial to automatic dependency tracking, calling the getter within a tracking scope causes the calling function to depend on this Signal, so that function will rerun if the Signal gets updated.
4040

41-
Calling the setter (e.g., `setCount(nextCount)` or `setReady(nextReady)`) sets the Signal's value and updates the Signal (triggering dependents to rerun) if the value actually changed (see details below).
42-
The setter takes either the new value for the signal or a function that maps the previous value of the signal to a new value as its only argument.
41+
Calling the setter (e.g., `setCount(nextCount)` or `setReady(nextReady)`) sets the Signal's value and updates the Signal (triggering dependents to rerun) if the value actually changed (see details below).
42+
The setter takes either the new value for the signal or a function that maps the previous value of the signal to a new value as its only argument.
4343
The updated value is also returned by the setter. As an example:
4444

4545
```tsx
@@ -81,11 +81,30 @@ const newCount = setCount((prev) => prev + 1)
8181

8282
## Options
8383

84-
| Name | Type | Default | Description |
85-
| ---------- | ------------------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
86-
| `equals` | `false \| ((prev: T, next: T) => boolean)` | `===` | A function that determines whether the Signal's value has changed. If the function returns true, the Signal's value will not be updated and dependents will not rerun. If the function returns false, the Signal's value will be updated and dependents will rerun. |
87-
| `name` | `string` | | A name for the Signal. This is useful for debugging. |
88-
| `internal` | `boolean` | `false` | If true, the Signal will not be accessible in the devtools. |
84+
### `equals`
85+
86+
**Type**: `false | ((prev: T, next: T) => boolean)`
87+
88+
**Default**: `===`
89+
90+
A function that determines whether the Signal's value has changed.
91+
If the function returns true, the Signal's value will not be updated and dependents will not rerun.
92+
If the function returns false, the Signal's value will be updated and dependents will rerun.
93+
94+
### `name`
95+
96+
**Type**: `string`
97+
98+
A name for the Signal.
99+
This is useful for debugging.
100+
101+
### `internal`
102+
103+
**Type**: `boolean`
104+
105+
**Default**: `false`
106+
107+
If true, the Signal will not be accessible in the devtools.
89108

90109
### `equals`
91110

src/routes/reference/components/dynamic.mdx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ Here's an example of how you can use it:
2525

2626
## Props
2727

28-
| Name | Type | Description |
29-
| :---------- | :---------------------------------------------------------- | :---------------------------------------- |
30-
| `component` | `Component<T>` \| `string` \| `keyof JSX.IntrinsicElements` | The component to render. |
31-
| `children` | `any` | The children to pass to the component. |
32-
| `...` | `T` | Any other props to pass to the component. |
28+
### `component`
29+
30+
**Type**: `Component<T> | string | keyof JSX.IntrinsicElements`
31+
32+
The component to render.
33+
34+
### `children`
35+
36+
Any other props to pass to the component.

src/routes/reference/components/for.mdx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,20 @@ The optional second argument is an index signal:
4444

4545
## Props
4646

47-
| Name | Type | Description |
48-
| :--------- | :------------------------------------ | :--------------------------------------------------------------- |
49-
| `each` | `readonly T[]` | The list of items to render. |
50-
| `fallback` | `JSX.Element` | A fallback element to render while the list is loading. |
51-
| `children` | `(item: T, index: () => number) => U` | A callback that returns a JSX element for each item in the list. |
47+
### `each`
48+
49+
**Type**: `readonly T[]`
50+
51+
The list of items to render.
52+
53+
### `fallback`
54+
55+
**Type**: `JSX.Element`
56+
57+
A fallback element to render while the list is loading.
58+
59+
### `children`
60+
61+
**Type**: `(item: T, index: () => number) => U`
62+
63+
A callback that returns a JSX element for each item in the list.

src/routes/reference/components/index-component.mdx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,20 @@ Optional second argument is an index number:
5454

5555
## Props
5656

57-
| Name | Type | Description |
58-
| :------- | :------------------------------------ | :-------------------------------------------------------------- |
59-
| each | `readonly T[]` | The array to iterate over. |
60-
| fallback | `JSX.Element` | Optional fallback element to render while the array is loading. |
61-
| children | `(item: () => T, index: number) => U` | The function that renders the children. |
57+
### `each`
58+
59+
**Type**: `readonly T[]`
60+
61+
The array to iterate over.
62+
63+
### `fallback`
64+
65+
**Type**: `JSX.Element`
66+
67+
Optional fallback element to render while the array is loading.
68+
69+
### `children`
70+
71+
**Type**: `(item: () => T, index: number) => U`
72+
73+
The function that renders the children.

0 commit comments

Comments
 (0)