Skip to content

Commit 728ce62

Browse files
committed
feat: better Store example
1 parent dc77051 commit 728ce62

File tree

5 files changed

+89
-88
lines changed

5 files changed

+89
-88
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { Store } from '@tanstack/store'
1+
import { Derived, Store } from '@tanstack/store'
22

33
export const store = new Store({
4-
count: 0,
4+
firstName: 'Jane',
5+
lastName: 'Smith',
56
})
7+
8+
export const fullName = new Derived({
9+
fn: () => `${store.state.firstName} ${store.state.lastName}`,
10+
deps: [store],
11+
})
12+
13+
fullName.mount()

templates/solid/add-on/store/assets/src/routes/demo.store.page1.tsx.ejs

Lines changed: 0 additions & 41 deletions
This file was deleted.

templates/solid/add-on/store/assets/src/routes/demo.store.page2.tsx.ejs

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/solid-router'
2+
import { useStore } from '@tanstack/solid-store'
3+
4+
import { fullName, store } from '../lib/demo-store'
5+
6+
<% if (codeRouter) { %>
7+
import type { RootRoute } from '@tanstack/react-router'
8+
<% } else { %>
9+
export const Route = createFileRoute('/demo/store')({
10+
component: DemoStore,
11+
})
12+
<% } %>
13+
14+
function FirstName() {
15+
const firstName = useStore(store, (state) => state.firstName)
16+
return (
17+
<input
18+
type="text"
19+
value={firstName()}
20+
onInput={(e) =>
21+
store.setState((state) => ({ ...state, firstName: e.target.value }))
22+
}
23+
class="bg-white/10 rounded-lg px-4 py-2 outline-none border border-white/20 hover:border-white/40 focus:border-white/60 transition-colors duration-200 placeholder-white/40"
24+
/>
25+
)
26+
}
27+
28+
function LastName() {
29+
const lastName = useStore(store, (state) => state.lastName)
30+
return (
31+
<input
32+
type="text"
33+
value={lastName()}
34+
onInput={(e) =>
35+
store.setState((state) => ({ ...state, lastName: e.target.value }))
36+
}
37+
class="bg-white/10 rounded-lg px-4 py-2 outline-none border border-white/20 hover:border-white/40 focus:border-white/60 transition-colors duration-200 placeholder-white/40"
38+
/>
39+
)
40+
}
41+
42+
function FullName() {
43+
const fName = useStore(fullName)
44+
return (
45+
<div class="bg-white/10 rounded-lg px-4 py-2 outline-none ">
46+
{fName}
47+
</div>
48+
)
49+
}
50+
51+
function DemoStore() {
52+
return (
53+
<div
54+
class="min-h-[calc(100vh-32px)] text-white p-8 flex items-center justify-center w-full h-full"
55+
style={{
56+
'background-image':
57+
'radial-gradient(50% 50% at 80% 80%, #f4a460 0%, #8b4513 70%, #1a0f0a 100%)',
58+
}}
59+
>
60+
<div class="bg-white/10 backdrop-blur-lg rounded-xl p-8 shadow-lg flex flex-col gap-4 text-3xl min-w-1/2">
61+
<h1 class="text-4xl font-bold mb-5">Store Example</h1>
62+
<FirstName />
63+
<LastName />
64+
<FullName />
65+
</div>
66+
</div>
67+
)
68+
}
69+
70+
<% if (codeRouter) { %>
71+
export default (parentRoute: RootRoute) => createRoute({
72+
path: '/demo/store',
73+
component: DemoStore,
74+
getParentRoute: () => parentRoute,
75+
})
76+
<% } %>
77+

templates/solid/add-on/store/info.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
"link": "https://tanstack.com/store/latest",
77
"routes": [
88
{
9-
"url": "/demo/store/page1",
10-
"name": "Store Pg 1"
11-
},
12-
{
13-
"url": "/demo/store/page2",
14-
"name": "Store Pg 2"
9+
"url": "/demo/store",
10+
"name": "Store"
1511
}
1612
]
1713
}

0 commit comments

Comments
 (0)