css - 将 React-Bootstrap xs,sm,md,lg,xl 跨度/偏移量保存到 css 类中
问题描述
我们有以下<Row>
两个<Col>
元素:
<Row>
<Col xs={12} sm={{ span: 9, offset: 1 }} md={{ span: 6, offset: 0 }} lg={5} xl={4}>
<h3 className='graph-header-1'>First Header</h3>
<div>Graph that fills the col-width goes here</div>
</Col>
<Col xs={12} sm={{ span: 9, offset: 1 }} md={{ span: 6, offset: 0 }} lg={{ span: 5, offset: 1 }} xl={{ span: 4, offset: 2 }}>
<h3 className='graph-header-1'></h3>
<div>2nd Graph that fills the column's width goes here</div>
</Col>
</Row>
这种布局(有 2 个列的行,每个 xs、sm、md、lg、xl 具有这些特定的跨度/偏移量)将在我们应用程序的几个地方使用。是否可以将其保存在一个类中,以便我们无需一遍又一遍地设置这些值,只需使用:
<Row className='our-responsive-layout'>
<Col>
<h3 className='graph-header-1'>First Header</h3>
<div>Graph that fills the col-width goes here</div>
</Col>
<Col>
<h3 className='graph-header-1'></h3>
<div>2nd Graph that fills the column's width goes here</div>
</Col>
</Row>
...our-responsive-layout
处理 2 列的跨度/偏移量的类在哪里?或者,为每列设置一个类(而不是为 2 列的行设置一个类)也会有所帮助。
编辑:如果有任何关于使用 react-bootstrap 及其容器/行/列组件在复杂的 react-app 中处理响应性的全面指南,请分享。我担心xs={12} sm={{ span: 9, offset: 1 }} md={{ span: 6, offset: 0 }} lg={{ span: 5, offset: 1 }} xl={{ span: 4, offset: 2 }}
在我们的应用程序中添加诸如此类的东西会使代码更加混乱。
谢谢!
解决方案
你为什么不简单地为此创建一个组件?类似的东西(演示):
import React, { Component } from "react";
import { render } from "react-dom";
const App = () => {
return (
<MyRow>
<MyCol>Here is your col 1</MyCol>
<MyCol>Here is your col 2</MyCol>
</MyRow>
);
};
const MyRow = ({children}) => {
return <div className='this is your row'>{children}</div>
}
const MyCol = ({children}) => {
return <div className='this is your col'>{children}</div>;
}
render(<App />, document.getElementById("root"));
在这里,您只需替换行和col 组件div
中的标签(当然还有 className),就足够了。MyRow
MyCol
例如,如果你想在 col 上添加或更改类,你可以添加一个 prop。
推荐阅读
- html - div-box 两侧的内容和前元素
- javascript - javascript FileAttachment 未定义
- sql-server - 当时间在给定的分钟间隔内连续时对记录进行分组
- java - 连续子数组。有比 O(n^2) 更好的解决方案吗?
- javascript - Uncaught (in promise) SyntaxError: Unexpected end of JSON input on my console
- excel - 使用 VBA 基于可变工作表编号创建多个数据透视表
- reactjs - 如何将我在反应功能组件中获取和映射的数据传递给反应模式?
- flutter - ckeckbox 不会改变状态
- mysql - 在 ubuntu 中使用 mysql 时出错访问被拒绝
- php - 您可以使用 catch 语句来发出警告吗:PHP