Open History to see the last commit
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 )
52}
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 )
101}
102```