svelte table does not seem to be reactive when pulling data from query
Hi guys, I have the following code
The table does not get updated when the data is updated, I understand I am passing the value and not the store which is what probably breaks the reactivity but it's the only way to pass it because the store is not the correct type (on the top level) and I can't pass a store to the table
import { getCoreRowModel, createSvelteTable, getSortedRowModel, type TableOptions, flexRender, type ColumnDef, type Table } from '@tanstack/svelte-table';
import { createQuery } from '@tanstack/svelte-query';
import { awaitTimeOut } from '$utils/awaitSleep';
import { usersList } from './data';
import { Chips, DateCell, Id } from '$components/tables/cells';
import { dateFormatter } from '$lib/formatters';
import type { Readable } from 'svelte/store';
interface User {
id: string;
firstName: string;
lastName: string;
email: string;
isOnline: boolean;
createdAt: string;
roles: string[];
}
async function getUsers() {
await awaitTimeOut(1000);
return usersList;
}
let users = createQuery({
queryKey: ['users'],
queryFn: getUsers,
initialData: [
{id: '031a9ead-acb1-446b-b4e7-b9b1cac125f4', firstName: 'Joellyn', lastName: 'Filipponi', email: 'jfilipponi0@soup.io', isOnline::false, roles: ['admin'], createdAt: '2022-01-12 10:07:07'},
],
onSuccess: (data) => {}
});
const columns: ColumnDef<User>[] = [
{
id: 'name',
accessorFn: (row) => `${row.firstName} ${row.lastName}`,
header: 'Name'
},
{
id: 'createdAt',
accessorFn: (row) => dateFormatter.format(new Date(row.createdAt)),
header: 'Created At',
cell: (cell) => flexRender(DateCell, { value: cell.getValue() })
},
{
id: 'roles',
accessorKey: 'roles',
header: 'Roles',
cell: (cell) => flexRender(Chips, { value: cell.getValue() })
}
];
const options: TableOptions<User> = {
data: $users.data,
columns,
getCoreRowModel: getCoreRowModel(),
enableRowSelection: true
};
let table: Readable<Table<User>> = createSvelteTable(options);
import { getCoreRowModel, createSvelteTable, getSortedRowModel, type TableOptions, flexRender, type ColumnDef, type Table } from '@tanstack/svelte-table';
import { createQuery } from '@tanstack/svelte-query';
import { awaitTimeOut } from '$utils/awaitSleep';
import { usersList } from './data';
import { Chips, DateCell, Id } from '$components/tables/cells';
import { dateFormatter } from '$lib/formatters';
import type { Readable } from 'svelte/store';
interface User {
id: string;
firstName: string;
lastName: string;
email: string;
isOnline: boolean;
createdAt: string;
roles: string[];
}
async function getUsers() {
await awaitTimeOut(1000);
return usersList;
}
let users = createQuery({
queryKey: ['users'],
queryFn: getUsers,
initialData: [
{id: '031a9ead-acb1-446b-b4e7-b9b1cac125f4', firstName: 'Joellyn', lastName: 'Filipponi', email: 'jfilipponi0@soup.io', isOnline::false, roles: ['admin'], createdAt: '2022-01-12 10:07:07'},
],
onSuccess: (data) => {}
});
const columns: ColumnDef<User>[] = [
{
id: 'name',
accessorFn: (row) => `${row.firstName} ${row.lastName}`,
header: 'Name'
},
{
id: 'createdAt',
accessorFn: (row) => dateFormatter.format(new Date(row.createdAt)),
header: 'Created At',
cell: (cell) => flexRender(DateCell, { value: cell.getValue() })
},
{
id: 'roles',
accessorKey: 'roles',
header: 'Roles',
cell: (cell) => flexRender(Chips, { value: cell.getValue() })
}
];
const options: TableOptions<User> = {
data: $users.data,
columns,
getCoreRowModel: getCoreRowModel(),
enableRowSelection: true
};
let table: Readable<Table<User>> = createSvelteTable(options);
1 Reply
sunny-green•3y ago
Having the same problem. I am curious if anyone found a solution to this?