Skip to content

useGridLayout

Track the number of rows and columns of a grid layout using this hook.

Parameters

  • the ref object of the target element.

Return Values

NameTypeDescription
refRefObjectthe target element reference.
rowsNumberthe grid rows number.
columnsNumberthe grid columns number.

Example Usage

tsx
import { useGridLayout } from 'react-pre-hooks';

export default function Example() {
  const { ref, rows, columns } = useGridLayout();

  return (
    <main>
      <p>
        Rows: {rows}, Columns: {columns}
      </p>
      <div ref={ref} style={{ display: 'grid' }}>
        {new Array(12).fill(null).map((_, i) => (
          <span key={i}></span>
        ))}
      </div>
    </main>
  );
}
import { useGridLayout } from 'react-pre-hooks';

export default function Example() {
  const { ref, rows, columns } = useGridLayout();

  return (
    <main>
      <p>
        Rows: {rows}, Columns: {columns}
      </p>
      <div ref={ref} style={{ display: 'grid' }}>
        {new Array(12).fill(null).map((_, i) => (
          <span key={i}></span>
        ))}
      </div>
    </main>
  );
}

Released under the MIT License.