react-grid-layout
约 1 分钟阅读
- react-grid-layout/react-grid-layoutGitHubreact-grid-layout/react-grid-layoutreact-grid-layout/react-grid-layout · 子项目 · react-draggable · react-resizable · Responsive · lg,md,sm,xs,xxs · memo children 可以提升性能笔记:react-grid-layout
- Responsive
- lg,md,sm,xs,xxs
- memo children 可以提升性能
- 自定义 child 要求 React.forwardRef 且接受 style, className
- ResponsiveGridLayout 在 container 中,当 container resize 的时候不会变化
- WidthProvideGitHub 文件react-grid-layout/react-grid-layout/lib/components/WidthProvider.jsx 默认只监听了 window resize
// 基于环境自动检测 width
const ResponsiveGridLayout = WidthProvider(Responsive);
const Demo = () => {
return (
<ResponsiveGridLayout
layouts={{}}
breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }}
>
<div key="1">1</div>
<div key="2">2</div>
<div key="3">3</div>
</ResponsiveGridLayout>
);
};Notes
- resize handle 会作为 children 传入 子元素
- 因此 children 会变为数组
- 自定义元素需要注意处理好 透传 props
FAQ
Resize based on container
import useResizeObserver from 'use-resize-observer';
import { Responsive, ResponsiveProps } from 'react-grid-layout';
const ResponsiveGridLayout: React.FC<ResponsiveProps> = (props) => {
const { ref, width } = useResizeObserver();
const realWidth = useDebounce(width, 200);
// measureBeforeMount
const w = realWidth ?? width;
return (
<div className={classNames('h-full w-full')} ref={ref}>
{w && <Responsive width={w} {...props} />}
</div>
);
};关联信息
反向链接、本文链接的其他页面和外部资料。
参考资料
GitHub4 条
- react-grid-layout/react-draggablegithub.com/react-grid-layout/react-draggable
- react-grid-layout/react-grid-layoutgithub.com/react-grid-layout/react-grid-layout
- react-grid-layout/react-grid-layout/lib/components/WidthProvider.jsxgithub.com/react-grid-layout/react-grid-layout/blob/master/lib/components/WidthProvider.jsx
- react-grid-layout/react-resizablegithub.com/react-grid-layout/react-resizable