-
-
Notifications
You must be signed in to change notification settings - Fork 98
feat: new hooks and actions APIs #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KevinVandy
wants to merge
19
commits into
main
Choose a base branch
from
hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3c6c1a1
chore: migrate builds to tsdown and upgrade all deps
KevinVandy 9e046ca
changeset
KevinVandy 7e4c084
clean up solid exports and node types overrides
KevinVandy 0a0ee5b
reapply exports in package jsons
KevinVandy 96a4510
add back yaml
KevinVandy df72c71
fix yaml config that was accidentally deleted
KevinVandy 936d771
format
KevinVandy df5c80d
feat: new set of react hooks
KevinVandy 638a213
Merge branch 'main' into hooks
KevinVandy 154e0a8
ci: apply automated fixes and generate docs
autofix-ci[bot] 47f9806
fix package.jsons
KevinVandy 92ec178
feat: add actions to stores (#307)
KevinVandy c5e4bdc
also bind subscribe
KevinVandy 44daa4c
replace `new Store` with createStore in examples
KevinVandy 4223af7
remove useValue, make selector optional in useSelector
KevinVandy e426852
add solid context helper
KevinVandy 206c624
update changeset
KevinVandy 9d697bf
format
KevinVandy 557ad06
improve store action examples
KevinVandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| '@tanstack/angular-store': minor | ||
| '@tanstack/preact-store': minor | ||
| '@tanstack/svelte-store': minor | ||
| '@tanstack/react-store': minor | ||
| '@tanstack/solid-store': minor | ||
| '@tanstack/vue-store': minor | ||
| '@tanstack/store': minor | ||
| --- | ||
|
|
||
| chore: update deps and change build process to tsdown | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
docs/framework/react/reference/functions/createStoreContext.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| id: createStoreContext | ||
| title: createStoreContext | ||
| --- | ||
|
|
||
| # Function: createStoreContext() | ||
|
|
||
| ```ts | ||
| function createStoreContext<TValue>(): object; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/createStoreContext.ts:40](https://github.com/TanStack/store/blob/main/packages/react-store/src/createStoreContext.ts#L40) | ||
|
|
||
| Creates a typed React context for sharing a bundle of atoms and stores with a subtree. | ||
|
|
||
| The returned `StoreProvider` only transports the provided object through | ||
| React context. Consumers destructure the contextual atoms and stores, then | ||
| compose them with the existing hooks like [useSelector](useSelector.md), | ||
| [useValue](useValue.md), [useSetValue](useSetValue.md), and [useAtom](useAtom.md). | ||
|
|
||
| The object shape is preserved exactly, so keyed atoms and stores remain fully | ||
| typed when read back with `useStoreContext()`. | ||
|
|
||
| ## Type Parameters | ||
|
|
||
| ### TValue | ||
|
|
||
| `TValue` *extends* `object` | ||
|
|
||
| ## Returns | ||
|
|
||
| `object` | ||
|
|
||
|
KevinVandy marked this conversation as resolved.
|
||
| ### StoreProvider() | ||
|
|
||
| ```ts | ||
| StoreProvider: (props) => ReactElement; | ||
| ``` | ||
|
|
||
| #### Parameters | ||
|
|
||
| ##### props | ||
|
|
||
| ###### children? | ||
|
|
||
| `ReactNode` | ||
|
|
||
| ###### value | ||
|
|
||
| `TValue` | ||
|
|
||
| #### Returns | ||
|
|
||
| `ReactElement` | ||
|
|
||
| ### useStoreContext() | ||
|
|
||
| ```ts | ||
| useStoreContext: () => TValue; | ||
| ``` | ||
|
|
||
| #### Returns | ||
|
|
||
| `TValue` | ||
|
|
||
| ## Example | ||
|
|
||
| ```tsx | ||
| const { StoreProvider, useStoreContext } = createStoreContext<{ | ||
| countAtom: Atom<number> | ||
| totalsStore: Store<{ count: number }> | ||
| }>() | ||
|
|
||
| function CountButton() { | ||
| const { countAtom, totalsStore } = useStoreContext() | ||
| const count = useValue(countAtom) | ||
| const total = useSelector(totalsStore, (state) => state.count) | ||
|
|
||
| return ( | ||
| <button | ||
| type="button" | ||
| onClick={() => totalsStore.setState((state) => ({ ...state, count: state.count + 1 }))} | ||
| > | ||
| {count} / {total} | ||
| </button> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Throws | ||
|
|
||
| When `useStoreContext()` is called outside the matching `StoreProvider`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| id: useAtom | ||
| title: useAtom | ||
| --- | ||
|
|
||
| # Function: useAtom() | ||
|
|
||
| ```ts | ||
| function useAtom<TValue>(atom, options?): [TValue, (fn) => void & (value) => void]; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/useAtom.ts:17](https://github.com/TanStack/store/blob/main/packages/react-store/src/useAtom.ts#L17) | ||
|
|
||
| Returns the current atom value together with a stable setter. | ||
|
|
||
| This is the writable-atom convenience hook for components that need to both | ||
| read and update the same atom. | ||
|
|
||
| ## Type Parameters | ||
|
|
||
| ### TValue | ||
|
|
||
| `TValue` | ||
|
|
||
| ## Parameters | ||
|
|
||
| ### atom | ||
|
|
||
| `Atom`\<`TValue`\> | ||
|
|
||
| ### options? | ||
|
|
||
| [`UseSelectorOptions`](../interfaces/UseSelectorOptions.md)\<`TValue`\> | ||
|
|
||
| ## Returns | ||
|
|
||
| \[`TValue`, (`fn`) => `void` & (`value`) => `void`\] | ||
|
|
||
| ## Example | ||
|
|
||
| ```tsx | ||
| const [count, setCount] = useAtom(countAtom) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| --- | ||
| id: useCreateAtom | ||
| title: useCreateAtom | ||
| --- | ||
|
|
||
| # Function: useCreateAtom() | ||
|
|
||
| ## Call Signature | ||
|
|
||
| ```ts | ||
| function useCreateAtom<T>(getValue, options?): ReadonlyAtom<T>; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/useCreateAtom.ts:17](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateAtom.ts#L17) | ||
|
|
||
| Creates a stable atom instance for the lifetime of the component. | ||
|
|
||
| Pass an initial value to create a writable atom, or a getter function to | ||
| create a readonly derived atom. This hook mirrors the overloads from | ||
| createAtom, but ensures the atom is only created once per mount. | ||
|
|
||
| ### Type Parameters | ||
|
|
||
| #### T | ||
|
|
||
| `T` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### getValue | ||
|
|
||
| (`prev?`) => `T` | ||
|
|
||
| #### options? | ||
|
|
||
| `AtomOptions`\<`T`\> | ||
|
|
||
| ### Returns | ||
|
|
||
| `ReadonlyAtom`\<`T`\> | ||
|
|
||
| ### Example | ||
|
|
||
| ```tsx | ||
| const countAtom = useCreateAtom(0) | ||
| ``` | ||
|
|
||
| ## Call Signature | ||
|
KevinVandy marked this conversation as resolved.
|
||
|
|
||
| ```ts | ||
| function useCreateAtom<T>(initialValue, options?): Atom<T>; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/useCreateAtom.ts:21](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateAtom.ts#L21) | ||
|
|
||
| Creates a stable atom instance for the lifetime of the component. | ||
|
|
||
| Pass an initial value to create a writable atom, or a getter function to | ||
| create a readonly derived atom. This hook mirrors the overloads from | ||
| createAtom, but ensures the atom is only created once per mount. | ||
|
|
||
| ### Type Parameters | ||
|
|
||
| #### T | ||
|
|
||
| `T` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### initialValue | ||
|
|
||
| `T` | ||
|
|
||
| #### options? | ||
|
|
||
| `AtomOptions`\<`T`\> | ||
|
|
||
| ### Returns | ||
|
|
||
| `Atom`\<`T`\> | ||
|
|
||
| ### Example | ||
|
|
||
| ```tsx | ||
| const countAtom = useCreateAtom(0) | ||
| ``` | ||
78 changes: 78 additions & 0 deletions
78
docs/framework/react/reference/functions/useCreateStore.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| --- | ||
| id: useCreateStore | ||
| title: useCreateStore | ||
| --- | ||
|
|
||
| # Function: useCreateStore() | ||
|
|
||
| ## Call Signature | ||
|
|
||
| ```ts | ||
| function useCreateStore<T>(getValue): ReadonlyStore<T>; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/useCreateStore.ts:17](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateStore.ts#L17) | ||
|
|
||
| Creates a stable store instance for the lifetime of the component. | ||
|
|
||
| Pass an initial value to create a writable store, or a getter function to | ||
| create a readonly derived store. This hook mirrors the overloads from | ||
| createStore, but ensures the store is only created once per mount. | ||
|
|
||
| ### Type Parameters | ||
|
|
||
| #### T | ||
|
|
||
| `T` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### getValue | ||
|
|
||
| (`prev?`) => `T` | ||
|
|
||
| ### Returns | ||
|
|
||
| `ReadonlyStore`\<`T`\> | ||
|
|
||
| ### Example | ||
|
|
||
| ```tsx | ||
| const counterStore = useCreateStore({ count: 0 }) | ||
| ``` | ||
|
|
||
| ## Call Signature | ||
|
KevinVandy marked this conversation as resolved.
|
||
|
|
||
| ```ts | ||
| function useCreateStore<T>(initialValue): Store<T>; | ||
| ``` | ||
|
|
||
| Defined in: [react-store/src/useCreateStore.ts:20](https://github.com/TanStack/store/blob/main/packages/react-store/src/useCreateStore.ts#L20) | ||
|
|
||
| Creates a stable store instance for the lifetime of the component. | ||
|
|
||
| Pass an initial value to create a writable store, or a getter function to | ||
| create a readonly derived store. This hook mirrors the overloads from | ||
| createStore, but ensures the store is only created once per mount. | ||
|
|
||
| ### Type Parameters | ||
|
|
||
| #### T | ||
|
|
||
| `T` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### initialValue | ||
|
|
||
| `T` | ||
|
|
||
| ### Returns | ||
|
|
||
| `Store`\<`T`\> | ||
|
|
||
| ### Example | ||
|
|
||
| ```tsx | ||
| const counterStore = useCreateStore({ count: 0 }) | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.