import { useState } from 'react' import { createFileRoute } from '@tanstack/react-router' import { usePowerSync, useQuery, useStatus } from '@powersync/react' export const Route = createFileRoute('/demo/powersync')({ component: PowerSyncDemo, }) type TodoRow = { id: string created_at: string description: string completed: number } function PowerSyncDemo() { const powerSync = usePowerSync() const status = useStatus() const { data } = useQuery( 'SELECT id, created_at, description, completed FROM todos ORDER BY created_at DESC', ) const todos = (data ?? []) as Array const [description, setDescription] = useState('') const [error, setError] = useState(null) async function addTodo(event: React.FormEvent) { event.preventDefault() const nextDescription = description.trim() if (!nextDescription) { return } try { setError(null) await powerSync.execute( 'INSERT INTO todos (id, created_at, description, completed) VALUES (?, ?, ?, ?)', [crypto.randomUUID(), new Date().toISOString(), nextDescription, 0], ) setDescription('') } catch (error) { console.error('Failed to insert PowerSync todo', error) setError('Failed to insert row. Please try again.') } } return (

Offline Sync

PowerSync

This demo writes to the local SQLite database immediately. Replace the sample schema and backend connector with your real PowerSync configuration.

Connection State

            {JSON.stringify(status, null, 2)}
          

Local Todos

{error ? (

{error}

) : null}
setDescription(event.target.value)} placeholder="Write to the local PowerSync database" value={description} />
    {todos.length === 0 ? (
  • No rows yet. Insert one locally, then wire `uploadData()` to send it upstream.
  • ) : ( todos.map((todo) => (
  • {todo.description}

    {todo.created_at}

    {todo.completed ? 'done' : 'pending'}
  • )) )}
) }