ziggy
ziggy

Reputation: 55

Is it possible to get initial breakpoint using react-grid-layout?

Using https://github.com/STRML/react-grid-layout

I need to know the current breakpoint on componentDidMount. Based on breakpoint, I know what item sizes I should provide (grid items are "picked up" by user from multiple select).

react-grid-layout provides API for updating breakpoint:

  onBreakpointChange = breakpoint => this.props.updateBreakpointKey(breakpoint);

But this funtion is triggered only on breakpoint changes. I need a solution to know what the current breakpoint is after component mounts.

Upvotes: 5

Views: 3279

Answers (3)

Mitanshu
Mitanshu

Reputation: 869

You can use window.innerWidth to get initial width and compare the value with defined breakpoints to get the cols.

import { useEffect, useState } from "react";
import { Responsive, WidthProvider } from "react-grid-layout";

import "react-grid-layout/css/styles.css";
import "react-resizable/css/styles.css";

const ROW_HEIGHT = 48;
const BREAKPOINTS = {
  lg: 1200,
  md: 996,
  sm: 768,
  xs: 480,
  xxs: 0,
};
const COLS = {
  lg: 16,
  md: 12,
  sm: 12,
  xs: 8,
  xxs: 4,
};

const ResponsiveGridLayout = WidthProvider(Responsive);

const Component = (props) => {
  const [cols, setCols] = useState();

  //To set initial cols value.
  useEffect(() => {
    const winWidth = window.innerWidth;
    for (let key in BREAKPOINTS) {
      if (winWidth > BREAKPOINTS[key] + 8) {
        //For me, breakpoints are working on (definedBreakpoints + 8) value.
        setCols(COLS[key]);
        break;
      }
    }
  }, []);

  const onBreakpointChange = (breakpoint, cols) => {
    setCols(cols);
  };

  return (
    <ResponsiveGridLayout
      onBreakpointChange={onBreakpointChange}
      breakpoints={BREAKPOINTS}
      cols={COLS}
      rowHeight={ROW_HEIGHT}
    >
      //...
    </ResponsiveGridLayout>
  );
};

export default Component;

Upvotes: 0

tonix
tonix

Reputation: 6939

As mentioned by CWSites, as soon as your component mounts (using componentDidMount) or within the onWidthChange() callback if you use the ResponsiveGridLayout component, you can do something like this:

import { Responsive, WidthProvider } from 'react-grid-layout';

const ResponsiveGridLayout = WidthProvider(Responsive);

class MyResponsiveGrid extends React.Component {

  initialBreakpoint = null;

  onWidthChange = width => {
    if (this.initialBreakpoint === null) {
      // The very first time.
      // Compute the initial breakpoint.
      const breakpoints = this.props.breakpoints;
      const sortedBreakpoints = Object.keys(breakpoints).sort(
          (breakpoint1, breakpoint2) =>
              breakpoints[breakpoint1] - breakpoints[breakpoint2]
      );
        let breakpoint = sortedBreakpoints[0];
        for (let i = 0; i < sortedBreakpoints.length; i++) {
            const currentBreakpoint = sortedBreakpoints[i];
            const nextBreakpoint = sortedBreakpoints[i + 1];
            if (
                typeof nextBreakpoint === "undefined" ||
                (breakpoints[currentBreakpoint] <= width &&
                    width <= breakpoints[nextBreakpoint])
            ) {
                breakpoint = currentBreakpoint;
                break;
            }
        }
        this.initialBreakpoint = breakpoint;

        // Call your method.
        this.props.updateBreakpointKey(this.initialBreakpoint)
    }
  }

  render() {
    // E.g.: {lg: layout1, md: layout2, ...}
    const layouts = this.props.layouts;

    // E.g.: {lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}
    const breakpoints = this.props.breakpoints;

    // E.g.: {lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}
    const cols = this.props.cols;

    return (
      <ResponsiveGridLayout layouts={layouts}
        breakpoints={breakpoints}
        cols={cols}
        onBreakpointChange={breakpoint => this.props.updateBreakpointKey(breakpoint)}
        onWidthChange={this.onWidthChange}>
        {this.props.children}
      </ResponsiveGridLayout>
    )
  }
}

Upvotes: 1

CWSites
CWSites

Reputation: 1801

Once your component mounts evaluate the props on <ResponsiveGridLayout> there is a prop for width which will return the current width of the component. From there you can compare that number against your breakpoints.

Example of props that are available once rendered.

<ResponsiveGridLayout 
  autoSize: true
  breakpoints: { lg: 1100, md: 768, sm: 0 }
  children: [...]
  cols: { lg: 12, md: 10, sm: 6 }
  layouts: { lg: [...], md: [...], sm: [...] }
  onBreakpointChange: onBreakpointChange()
  onLayoutChange: onLayoutChange()
  verticalCompact: true
  width: 1180
>

Upvotes: 1

Related Questions