| 55 | } |
| 56 | |
| 57 | export function useReactTable<TData extends RowData>( |
| 58 | options: TableOptions<TData>, |
| 59 | ) { |
| 60 | // Compose in the generic options to the user options |
| 61 | const resolvedOptions: TableOptionsResolved<TData> = { |
| 62 | state: {}, // Dummy state |
| 63 | onStateChange: () => {}, // noop |
| 64 | renderFallbackValue: null, |
| 65 | ...options, |
| 66 | } |
| 67 | |
| 68 | // Create a new table and store it in state |
| 69 | const [tableRef] = React.useState(() => ({ |
| 70 | current: createTable<TData>(resolvedOptions), |
| 71 | })) |
| 72 | |
| 73 | // By default, manage table state here using the table's initial state |
| 74 | const [state, setState] = React.useState(() => tableRef.current.initialState) |
| 75 | |
| 76 | // Compose the default state above with any user state. This will allow the user |
| 77 | // to only control a subset of the state if desired. |
| 78 | tableRef.current.setOptions((prev) => ({ |
| 79 | ...prev, |
| 80 | ...options, |
| 81 | state: { |
| 82 | ...state, |
| 83 | ...options.state, |
| 84 | }, |
| 85 | // Similarly, we'll maintain both our internal state and any user-provided |
| 86 | // state. |
| 87 | onStateChange: (updater) => { |
| 88 | setState(updater) |
| 89 | options.onStateChange?.(updater) |
| 90 | }, |
| 91 | })) |
| 92 | |
| 93 | return tableRef.current |
| 94 | } |