首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以使用react-grid-layout获取初始断点吗?

可以使用react-grid-layout获取初始断点吗?
EN

Stack Overflow用户
提问于 2019-02-14 00:19:30
回答 3查看 1.4K关注 0票数 4

使用https://github.com/STRML/react-grid-layout

我需要知道componentDidMount上的当前断点。基于断点,我知道我应该提供什么项目大小(网格项目是由用户从多个选择中“选取”的)。

react-grid-layout提供了更新断点的接口:

代码语言:javascript
复制
  onBreakpointChange = breakpoint => this.props.updateBreakpointKey(breakpoint);

但此函数仅在断点更改时触发。我需要一个解决方案来了解组件挂载后当前的断点是什么。

EN

回答 3

Stack Overflow用户

发布于 2019-02-15 01:06:27

一旦你的组件挂载,评估<ResponsiveGridLayout>上的道具,就会有一个width的道具,它将返回组件的当前宽度。从那里,您可以将该数字与您的断点进行比较。

渲染后可用的道具示例。

代码语言:javascript
复制
<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
>
票数 1
EN

Stack Overflow用户

发布于 2020-01-06 05:25:21

正如CWSites所提到的,一旦您的组件挂载(使用componentDidMount)或在onWidthChange()回调中(如果您使用ResponsiveGridLayout组件),您就可以执行以下操作:

代码语言:javascript
复制
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>
    )
  }
}
票数 1
EN

Stack Overflow用户

发布于 2021-10-26 03:58:39

您可以使用window.innerWidth获取初始宽度,并将该值与定义的断点进行比较,以获得cols。

代码语言:javascript
复制
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;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54674846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档