Open History to see the last commit
1---
2title: Quick Start
3id: quick-start
4---
5β
6The basic preact app example to get started with the TanStack preact-store.
7β
8```tsx
9import { render } from "preact";
10import { createStore, useSelector } from "@tanstack/preact-store";
11β
12// You can instantiate the store outside of Preact components too!
13export const store = createStore({
14 dogs: 0,
15 cats: 0,
16});
17β
18// This will only re-render when `state[animal]` changes. If an unrelated store property changes, it won't re-render
19β
20const Display = ({ animal }) => {
21 const count = useSelector(store, (state) => state[animal]);
22 return <div>{`${animal}: ${count}`}</div>;
23};
24β
25const updateState = (animal) => {
26 store.setState((state) => {
27 return {
28 ...state,
29 [animal]: state[animal] + 1,
30 };
31 });
32};
33const Increment = ({ animal }) => (
34 <button onClick={() => updateState(animal)}>My Friend Likes {animal}</button>
35);
36β
37function App() {
38 return (
39 <div>
40 <h1>How many of your friends like cats or dogs?</h1>
41 <p>
42 Press one of the buttons to add a counter of how many of your friends
43 like cats or dogs
44 </p>
45 <Increment animal="dogs" />
46 <Display animal="dogs" />
47 <Increment animal="cats" />
48 <Display animal="cats" />
49 </div>
50 );
51}
52β
53render(<App />, document.getElementById("root"));
54```
55β
56`useStore` remains available as a deprecated alias to `useSelector`.