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