Skip to content
Source
Sign inSign up
TanStack/store
Open History to see the last commit

index.tsx

62 lines1.2 KB

1import { Store, useStore } from '@tanstack/solid-store'
2import { render } from 'solid-js/web'
3​
4// You can instantiate a Store outside of Solid components too!
5export const store = new Store({
6 cats: 0,
7 dogs: 0,
8})
9​
10interface DisplayProps {
11 animals: 'dogs' | 'cats'
13​
14export const Display = (props: DisplayProps) => {
15 const count = useStore(store, (state) => state[props.animals])
16 return (
17 <div>
18 {props.animals}: {count()}
19 </div>
20 )
22​
23interface ButtonProps {
24 animals: 'dogs' | 'cats'
26​
27export const Button = (props: ButtonProps) => {
28 return (
29 <button
30 onClick={() => {
31 store.setState((state) => {
32 return {
33 ...state,
34 [props.animals]: state[props.animals] + 1,
35 }
36 })
37 }}
38 >
39 Increment
40 </button>
41 )
43​
44const App = () => {
45 return (
46 <div>
47 <h1>How many of your friends like cats or dogs?</h1>
48 <p>
49 Press one of the buttons to add a counter of how many of your friends
50 like cats or dogs
51 </p>
52 <Button animals="dogs" />
53 <Display animals="dogs" />
54 <Button animals="cats" />
55 <Display animals="cats" />
56 </div>
57 )
59​
60const root = document.getElementById('root')
61​
62render(() => <App />, root!)