Skip to content

Commit 11a8727

Browse files
committed
TS Store docs added
1 parent f9e7f25 commit 11a8727

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

project-template/README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ To run this example:
55
- `npm install` or `yarn` or `pnpm install` or `bun install`
66
- `npm start` or `yarn start` or `pnpm start` or `bun run dev`
77

8-
## Styling
9-
10-
This project includes [Tailwind CSS](https://tailwindcss.com/), a utility-first CSS framework.
11-
128
## Routing
139

1410
This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the src/main.tsx file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) guide.
@@ -360,3 +356,80 @@ export default App;
360356
```
361357

362358
You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
359+
360+
## State Management
361+
362+
Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.
363+
364+
First you need to add TanStack Store as a dependency:
365+
366+
```bash
367+
npm install @tanstack/store
368+
```
369+
370+
Now let's create a simple counter in the `src/App.tsx` file as a demonstration.
371+
372+
```tsx
373+
import { useStore } from "@tanstack/react-store";
374+
import { Store } from "@tanstack/store";
375+
import "./App.css";
376+
377+
const countStore = new Store(0);
378+
379+
function App() {
380+
const count = useStore(countStore);
381+
return (
382+
<div>
383+
<button onClick={() => countStore.setState((n) => n + 1)}>
384+
Increment - {count}
385+
</button>
386+
</div>
387+
);
388+
}
389+
390+
export default App;
391+
```
392+
393+
One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
394+
395+
Let's check this out by doubling the count using derived state.
396+
397+
```tsx
398+
import { useStore } from "@tanstack/react-store";
399+
import { Store, Derived } from "@tanstack/store";
400+
import "./App.css";
401+
402+
const countStore = new Store(0);
403+
404+
const doubledStore = new Derived({
405+
fn: () => countStore.state * 2,
406+
deps: [countStore],
407+
});
408+
doubledStore.mount();
409+
410+
function App() {
411+
const count = useStore(countStore);
412+
const doubledCount = useStore(doubledStore);
413+
414+
return (
415+
<div>
416+
<button onClick={() => countStore.setState((n) => n + 1)}>
417+
Increment - {count}
418+
</button>
419+
<div>Doubled - {doubledCount}</div>
420+
</div>
421+
);
422+
}
423+
424+
export default App;
425+
```
426+
427+
We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
428+
429+
Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
430+
431+
You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
432+
433+
# Learn More
434+
435+
You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).

0 commit comments

Comments
 (0)