@@ -31,7 +31,8 @@ export function flexRender<TProps extends object>(
3131}
3232
3333export function useQwikTable < TData extends RowData > (
34- options : TableOptions < TData >
34+ options : TableOptions < TData > ,
35+ existingTable ?: Table < TData >
3536) {
3637 // Compose in the generic options to the user options
3738 const resolvedOptions : TableOptionsResolved < TData > = {
@@ -41,32 +42,38 @@ export function useQwikTable<TData extends RowData>(
4142 ...options ,
4243 }
4344
44- // Create a new table instance and store it in a Qwik store
45- const table = Qwik . useStore < {
46- instance : Qwik . NoSerialize < Table < TData > >
45+ // Create or reuse the table instance and store it.
46+ const tableStore = Qwik . useStore < {
47+ instance : Qwik . NoSerialize < Table < TData > > | null
4748 } > ( {
48- instance : Qwik . noSerialize ( createTable ( resolvedOptions ) ) ,
49+ instance : null ,
4950 } )
5051
52+ if ( ! tableStore . instance ) {
53+ tableStore . instance = Qwik . noSerialize (
54+ existingTable ?? createTable ( resolvedOptions )
55+ )
56+ }
57+
58+ const table = tableStore . instance !
59+
5160 // By default, manage table state here using the table's initial state
52- const state = Qwik . useSignal ( table . instance ! . initialState )
61+ const state = Qwik . useSignal ( table . initialState )
5362
5463 // Compose the default state above with any user state. This will allow the user
5564 // to only control a subset of the state if desired.
56- table . instance ! . setOptions ( prev => ( {
65+ table . setOptions ( prev => ( {
5766 ...prev ,
5867 ...options ,
5968 state : {
6069 ...state . value ,
6170 ...options . state ,
6271 } ,
63- // Similarly, we'll maintain both our internal state and any user-provided
64- // state.
6572 onStateChange : updater => {
6673 state . value = updater instanceof Function ? updater ( state . value ) : updater
6774 options . onStateChange ?.( updater )
6875 } ,
6976 } ) )
7077
71- return table . instance !
78+ return table
7279}
0 commit comments