MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / App

Function App

templates/solid-ts/src/App.tsx:5–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { useSpacetimeDB, useTable, useReducer } from 'spacetimedb/solid';
4
5function App() {
6 const [name, setName] = createSignal('');
7
8 const conn = useSpacetimeDB();
9
10 // Subscribe to all people in the database
11 const [people] = useTable(() => tables.person);
12
13 const addReducer = useReducer(reducers.add);
14
15 const addPerson = (e: Event) => {
16 e.preventDefault();
17 if (!name().trim() || !conn.isActive) return;
18
19 // Call the add reducer
20 addReducer({ name: name() });
21 setName('');
22 };
23
24 return (
25 <div style={{ padding: '2rem' }}>
26 <h1>SpacetimeDB SolidJS App</h1>
27
28 <div style={{ 'margin-bottom': '1rem' }}>
29 Status:{' '}
30 <strong style={{ color: conn.isActive ? 'green' : 'red' }}>
31 {conn.isActive ? 'Connected' : 'Disconnected'}
32 </strong>
33 </div>
34
35 <form onSubmit={addPerson} style={{ 'margin-bottom': '2rem' }}>
36 <input
37 type="text"
38 placeholder="Enter name"
39 value={name()}
40 onInput={e => setName(e.currentTarget.value)}
41 style={{ padding: '0.5rem', 'margin-right': '0.5rem' }}
42 disabled={!conn.isActive}
43 />
44 <button
45 type="submit"
46 style={{ padding: '0.5rem 1rem' }}
47 disabled={!conn.isActive}
48 >
49 Add Person
50 </button>
51 </form>
52
53 <div>
54 <h2>People ({people.length})</h2>
55 <Show
56 when={people.length > 0}
57 fallback={<p>No people yet. Add someone above!</p>}
58 >
59 <ul>
60 <For each={people}>
61 {(person) => <li>{person.name}</li>}
62 </For>

Callers

nothing calls this directly

Calls 4

useSpacetimeDBFunction · 0.90
useTableFunction · 0.90
useReducerFunction · 0.90
nameFunction · 0.50

Tested by

no test coverage detected