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

useCreateAtom.md

84 lines1.5 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: [packages/react-store/src/useCreateAtom.ts:19](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateAtom.ts#L19)
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 hook mirrors the overloads from
18createAtom, but ensures the atom is only created once per 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`\<`T`\>
35​
36### Returns
37​
38`ReadonlyAtom`\<`T`\>
39​
40### Example
41​
42```tsx
43const countAtom = useCreateAtom(0)
44```
45​
46## Call Signature
47​
48```ts
49function useCreateAtom<T>(initialValue, options?): Atom<T>;
50```
51​
52Defined in: [packages/react-store/src/useCreateAtom.ts:23](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateAtom.ts#L23)
53​
54Creates a stable atom instance for the lifetime of the component.
55​
56Pass an initial value to create a writable atom, or a getter function to
57create a readonly derived atom. This hook mirrors the overloads from
58createAtom, but ensures the atom is only created once per mount.
59​
60### Type Parameters
61​
62#### T
63​
64`T`
65​
66### Parameters
67​
68#### initialValue
69​
70`T`
71​
72#### options?
73​
74`AtomOptions`\<`T`\>
75​
76### Returns
77​
78`Atom`\<`T`\>
79​
80### Example
81​
82```tsx
83const countAtom = useCreateAtom(0)
84```