In my Zustand store, I am storing items of type GridRow<T>
. These items represent selected rows in a main grid, with the use of generics to specify the data used in the grid.
I am facing a challenge on how to pass the generic parameter to the store's create
method. It is not feasible to wrap it in another function as it would result in creating a new store every time.
interface MainGridState<T> {
selectedRows: Array<GridRow<T>>
}
const initialState = {
selectedRows: []
}
// The issue arises here as T is undefined and there is no way to declare it explicitly
const useMainGridStore = create<MainGridState<T>>()(set => ({
...initialState,
}))
export const useMainGridSelectedRows = <T>(): Array<GridRow<T>> =>
useMainGridStore<T>(state => state.selectedRows)