Skip to content

Commit 7312d59

Browse files
authored
improve docs on untrack (#916)
1 parent ac1d02b commit 7312d59

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/routes/reference/reactive-utilities/untrack.mdx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,49 @@
22
title: untrack
33
---
44

5-
```ts
5+
Ignores tracking any of the dependencies in the executing code block and returns the value. This helper is useful when a certain `prop` will never update and thus it is ok to use it outside of the reactive context.
6+
7+
```tsx title="component.tsx"
68
import { untrack } from "solid-js"
79

8-
function untrack<T>(fn: () => T): T
10+
export function Component(props) {
11+
const value = untrack(() => props.value)
12+
13+
return <div>{value}</div>
14+
}
15+
}
16+
```
17+
18+
## Initial and Default Values
19+
20+
It is not necessary to manually untrack values that are suppose to serve as a default or initial value to a signal. Even with the linter configured to enforce tracking, the linter will accept it when a `prop` is prefixed with `default` or `initial` as it is a common pattern to use them as such.
21+
22+
<TabsCodeBlocks>
23+
<div id="initialValue">
924

25+
```tsx title="component.tsx" {4}
26+
import { createSignal } from "solid-js"
27+
28+
export function Component(props) {
29+
const [name, setName] = createSignal(props.initialName)
30+
31+
return <div>{name()}</div>
32+
}
33+
}
1034
```
35+
</div>
36+
37+
<div id="defaultValue">
1138

12-
Ignores tracking any of the dependencies in the executing code block and returns the value.
39+
```tsx title="component.tsx" {4}
40+
import { createSignal } from "solid-js"
41+
42+
export function Component(props) {
43+
const [name, setName] = createSignal(props.defaultName)
44+
45+
return <div>{name()}</div>
46+
}
47+
}
48+
```
49+
</div>
50+
</TabsCodeBlocks>

0 commit comments

Comments
 (0)