5.0
API
Hooks
useReactGridAPI

useReactGridAPI

The useReactGridAPI hook provides a set of methods to interact with the ReactGrid component. It allows you to set and get various states of the grid, such as the selected area, focused cell, and selected rows or columns. This hook is essential for managing the grid's state programmatically.

function useReactGridAPI(id: string): ReactGridAPI | undefined {
  return useReactGridStoreApi(id, (store: ReactGridStore) => {
    return {
      // Setters
      setSelectedArea: (range: NumericalRange) => {
        return store.setSelectedArea(range);
      },
      setFocusedCell: ({ rowIndex, colIndex }: IndexedLocation) => {
        const cell = store.getCellByIndexes(rowIndex, colIndex);
 
        if (devEnvironment && !cell) {
          console.warn(`Cell with rowIdx "${rowIndex}" and colIdx "${colIndex}" does not exist.`);
        }
 
        return store.setFocusedLocation(rowIndex, colIndex);
      },
      setSelectedColumns: (startColIdx: number, endColIdx: number) => {
        return store.setSelectedColumns(startColIdx, endColIdx);
      },
      setSelectedRows: (startRowIdx: number, endRowIdx: number) => {
        return store.setSelectedRows(startRowIdx, endRowIdx);
      },
 
      // Getters
      getFocusedCell: store.getFocusedCell,
      getCellByIndexes: store.getCellByIndexes,
      getCellOrSpanMemberByIndexes: store.getCellOrSpanMemberByIndexes,
      getPaneRanges: store.getPaneRanges,
      getCellsLookup: store.getCellsLookup,
      getSelectedArea: store.getSelectedArea,
      getRows: () => store.rows,
      getColumns: () => store.columns,
    };
  });
}
 

API Methods

NameTypeDescription
setSelectedArea(range: NumericalRange) => voidSet the selected area in the ReactGrid.
setFocusedCell({ rowIndex, colIndex }: IndexedLocation) => voidSet the focused cell in the ReactGrid.
setSelectedColumns(startColIdx: number, endColIdx: number) => voidSet the selected columns in the ReactGrid.
setSelectedRows(startRowIdx: number, endRowIdx: number) => voidSet the selected rows in the ReactGrid.
getFocusedCell() => IndexedLocation | undefinedGet the currently focused cell in the ReactGrid.
getCellByIndexes(rowIndex: number, colIndex: number) => Cell | undefinedGet the cell at the specified indexes in the ReactGrid.
getCellOrSpanMemberByIndexes(rowIndex: number, colIndex: number) => Cell | SpanMember | undefinedGet the cell or span member at the specified indexes in the ReactGrid.
getPaneRanges() => PaneRange[]Get the pane ranges in the ReactGrid.
getCellsLookup() => CellsLookupGet the cells lookup in the ReactGrid.
getSelectedArea() => NumericalRange | undefinedGet the selected area in the ReactGrid.
getRowsColumn[]Get the rows in the ReactGrid.
getColumnsRow[]Get the columns in the ReactGrid.