Skip to content

Commit

Permalink
docs(useStore): add react reference docs (#17)
Browse files Browse the repository at this point in the history
* docs(useStore): add react docs

This PR add the useStore react hook reference docs and explains the hook API.

* simplify usestore docs

---------

Co-authored-by: Vikas <vkumar3@snapchat.com>
  • Loading branch information
vikaskumar89 and vkumar3-sc committed Sep 2, 2023
1 parent e6bf5c9 commit c83f164
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/framework/react/reference/useStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,27 @@
title: Use Store
id: useStore
---
### `useStore`

```tsx
export function useStore<
TState,
TSelected = NoInfer<TState>,
TUpdater extends AnyUpdater = AnyUpdater,
>(
store: Store<TState, TUpdater>,
selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,
)
```

useStore is a custom hook that returns the updated store given the intial store and the selector. React can use this to keep your component subscribed to the store and re-render it on changes.



#### Parameters
- `store: Store<TState, TUpdater>`
- This parameter represents the external store itself which holds the entire state of your application. It expects an instance of a `@tanstack/store` that manages state and supports updates through a callback.
- `selector: (state: NoInfer<TState>) => TSelected = (d) => d as any`
- This parameter is a callback function that takes the state of the store and expects you to return a sub-state of the store. This selected sub-state is subsequently utilized as the state displayed by the useStore hook. It triggers a re-render of the component only when there are changes in this data, ensuring that updates to the displayed state trigger the necessary re-renders


Please see [/packages/react-store/src/index.tsx](https://github.com/tanstack/store/tree/main/packages/react-store/src/index.tsx)

0 comments on commit c83f164

Please sign in to comment.