Skip to content

Commit

Permalink
Don't directly reference local state
Browse files Browse the repository at this point in the history
In svelte 5, $state referenced locally is not reactive, because of how evaluation occurs.

Svelte will warn about this at runtime (and does so here)
To make it reactive locally, we use property getters, to ensure we always return the latest state.
  • Loading branch information
dberlin committed Mar 10, 2025
1 parent b5339c5 commit ca6e5d7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/svelte-table/src/createTable.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function createTable<
tableOptions,
{
_features,
state: { ...state, ...tableOptions.state },
get state() {
return { ...state, ...tableOptions.state }
},
mergeOptions: (
defaultOptions: TableOptions<TFeatures, TData>,
newOptions: Partial<TableOptions<TFeatures, TData>>,
Expand All @@ -40,7 +42,9 @@ export function createTable<
function updateOptions() {
table.setOptions((prev) => {
return mergeObjects(prev, tableOptions, {
state: mergeObjects(state, tableOptions.state || {}),
get state() {
return mergeObjects(state, tableOptions.state || {})
},
onStateChange: (updater: any) => {
if (isFunction(updater)) state = updater(state)
else state = mergeObjects(state, updater)
Expand Down

0 comments on commit ca6e5d7

Please sign in to comment.