Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

useCreateAtom.md

102 lines1.9 KB

1---
2id: useCreateAtom
3title: useCreateAtom
4---
5​
6## Call Signature
7​
8```ts
9function useCreateAtom<T>(getValue, options?): ReadonlyAtom<T>;
10```
11​
12Defined in: node\_modules/.pnpm/@tanstack+preact-store@0.13.2\_preact@10.29.8/node\_modules/@tanstack/preact-store/dist/useCreateAtom.d.ts:25
13​
14Creates a stable atom instance for the lifetime of the component.
15​
16Pass an initial value to create a writable atom, or a getter function to
17create a readonly derived atom. This mirrors [createAtom](createAtom.md), but only
18creates the atom once per component mount.
19​
20### Type Parameters
21​
22#### T
23​
24`T`
25​
26### Parameters
27​
28#### getValue
29​
30(`prev?`) => `T`
31​
32#### options?
33​
34[`AtomOptions`](../interfaces/AtomOptions.md)\<`T`\>
35​
36### Returns
37​
38[`ReadonlyAtom`](../interfaces/ReadonlyAtom.md)\<`T`\>
39​
40### Example
41​
42```tsx
43function Counter() {
44 const countAtom = useCreateAtom(0)
45 const [count, setCount] = useAtom(countAtom)
46​
47 return (
48 <button type="button" onClick={() => setCount((prev) => prev + 1)}>
49 {count}
50 </button>
51 )
53```
54​
55## Call Signature
56​
57```ts
58function useCreateAtom<T>(initialValue, options?): Atom<T>;
59```
60​
61Defined in: node\_modules/.pnpm/@tanstack+preact-store@0.13.2\_preact@10.29.8/node\_modules/@tanstack/preact-store/dist/useCreateAtom.d.ts:26
62​
63Creates a stable atom instance for the lifetime of the component.
64​
65Pass an initial value to create a writable atom, or a getter function to
66create a readonly derived atom. This mirrors [createAtom](createAtom.md), but only
67creates the atom once per component mount.
68​
69### Type Parameters
70​
71#### T
72​
73`T`
74​
75### Parameters
76​
77#### initialValue
78​
79`T`
80​
81#### options?
82​
83[`AtomOptions`](../interfaces/AtomOptions.md)\<`T`\>
84​
85### Returns
86​
87[`Atom`](../interfaces/Atom.md)\<`T`\>
88​
89### Example
90​
91```tsx
92function Counter() {
93 const countAtom = useCreateAtom(0)
94 const [count, setCount] = useAtom(countAtom)
95​
96 return (
97 <button type="button" onClick={() => setCount((prev) => prev + 1)}>
98 {count}
99 </button>
100 )
102```