如何在 React-Bootstrap 组件中将填充/边距作为props传递

IT技术 reactjs twitter-bootstrap-3 bootstrap-4 react-bootstrap
2021-05-20 22:28:02

我正在尝试使用 React-Bootstrap 作为props应用边距和填充。

我通过了文档,但没有发现任何提及在那里添加填充或边距,因为它在官方引导文档(第 3第 4 次)中。我知道它不能很好地支持 Bootstrap 4,所以尝试了两者。

我试图将 params 传递为p={1}paddingxs={5}或者mt='1'但它无法识别其中任何一个。更多的尝试在 React-Bootstrap 文件夹中找到任何Spacing元素,但失败了。

填充和边距用作类名。但是我觉得没有 Bootstrap 类就一定有办法。一定有一种财产。

4个回答

首先在您的src/index.jsApp.js

import 'bootstrap/dist/css/bootstrap.min.css';

然后你可以通过className在 React 中传递所需的引导 CSS 类名作为prop来设置你的组件的样式,例如:

import React from "react"
import Container from "react-bootstrap/Container";

function MyComponent() {
  return (
    <Container fluid className="p-0">
      <SomeOtherComponent />
    </Container>
  );
}

export default MyComponent

上面的代码会将p-0CSS 类添加Container.

参考

React - 如何向组件添加 CSS 类?

React-Bootstrap - 样式表

您可以使用默认的 React 样式添加边距和填充:

const divStyle = {
  marginLeft: '10px',
};

function HelloWorldComponent() {
  return <div style={divStyle}>Hello World!</div>;
}

这里引用

答案是:React Bootstrap 没有使用边距/填充的props。

您可以将props用于 col 类,但不能用于边距。

例子:

<Col className="col-6 col-md-3 mb-3 pt-2">
// there you have a Col component from React-Bootstrap 4
// it has some grid system classes, that you can use as props like this:

https://react-bootstrap.github.io/layout/grid/

<Col xs={6} md={3} className="mb-3 pt-2">
// but as you can see, the native classes of Bootstrap 4 like
// mt, mb, pt, pb etc, they have not a props use with
// React-Bootstrap, you have to use them like regular classes
// inside "className" 

您需要将 className="p-5" 添加到元素中。这在 react-bootstrap 中没有记录,但在原始 Bootstrap 4 文档中:https : //getbootstrap.com/docs/4.4/utilities/spacing/#examples