【问题标题】:Apply external custom css to a reactstrap input将外部自定义 css 应用于 reactstrap 输入
【发布时间】:2021-05-12 09:08:36
【问题描述】:

我在将来自外部 css 模块的自定义 css 应用到来自 Reactstrap 的输入时遇到问题。 所以我有以下代码:

<Input
            className={'border-0 p-0'}
            style={{
              width: '150px',
              height: '100px',
              fontSize: '18px',
              overflow: 'hidden',
              resize: 'none',
              backgroundColor: 'ffffff',
              boxShadow: 'none',
              marginTop: '-10px',
              marginBottom: '10px'
            }}
           innerRef={register}
            type={'textarea'}
            readOnly
          />

现在可以使用内联样式,但是属性太多,我想将它们放在 scss 外部模块中,但它不起作用。 我试过style={styles.customCss} 但它不起作用,我的自定义样式没有应用。我也尝试过输入className={'border-0 p-0' &amp;&amp; styles.customCss},但仍然无法正常工作。 有什么想法吗?

在我的 Css 外部文件中

.customCss {
  width: '150px';
  height: '100px';
  font-size: '18px';
  overflow: 'hidden';
  resize: 'none';
  background-color: 'ffffff';
  box-shadow: 'none';
  margin-top: '-10px';
  margin-bottom: '10px'
}

在我的组件中我有

import React from 'react';
import { useForm } from 'react-hook-form';
import {
  Container,
  Row,
  Col,
  Label,
  Input,
  Button,
  FormText
} from 'reactstrap';
import classnames from 'classnames/bind';

import styles from './styles.module.scss';

const cx = classnames.bind(styles);

const customComponent = props => {
  const { register, handleSubmit } = useForm();

  return (
    <div className={styles.container}>
      <div className={styles.heading}>
        <span className={styles.label}>
          {heading}
        </span>
      </div>
      <form>
        <Col className={'p-0'}>
          <Input
            className={cx(styles.customCss, 'border-0 p-0')}
            id="customer"
            name="customer"
            defaultValue={lot}
            innerRef={register}
            type={'textarea'}
            readOnly
          />
        </Col>
      </form>
    </div>
  );
};

export default customComponent;

【问题讨论】:

    标签: reactjs reactstrap


    【解决方案1】:

    我假设您在导入样式时正在使用 CSS 模块,即您的文件顶部有一个 import styles from './styles.module.scss'

    styles.customCss 将包含实际的 CSS 类名称,而不是完整的样式对象本身。因此,解决您使用style={styles.customCss} 的尝试,这是行不通的。请参阅 CRA 的 page on CSS modules

    现在,对于您使用 className={'border-0 p-0' &amp;&amp; styles.customCss} 的其他尝试,我知道您正在尝试将自定义 CSS 和 Bootstrap 类中的类名连接起来。代码实际上做的是对'border-0 p-0' &amp;&amp; styles.customCss 执行短路评估,它只会评估为styles.customCss。对于在 className 属性中组合 CSS 类,通常像 clsx 这样的库会很有帮助。

    更新

    看起来您的 CSS 文件有误。您正在使用 CSS,即这些值没有引号。这意味着,您的样式将是这样的:

    .customCss {
      width: 150px;
      height: 100px;
      font-size: 18px;
      overflow: hidden;
      resize: none;
      background-color: #ffffff;
      box-shadow: none;
      margin-top: -10px;
      margin-bottom: 10px;
    }
    

    sandbox demo 中的工作示例

    【讨论】:

    • 感谢您的回复。是的,就像您提到的那样,我在文件顶部有一个导入。我试过className={cx(styles.customCss, 'border-0 p-0')},但还是不行,我的customCss 样式没有应用。我也试过className={styles.customerAddress},但没有成功。
    • @Cristocel21 您能否在帖子中包含相关代码 sn-ps,即样式文件、组件的相关部分(导入语句等)?
    • 我已经用代码 sn-ps 编辑了我上面的问题
    • @Cristocel21 编辑了我的答案以解决您的新 sn-ps
    • 我已经修改了css,样式仍然没有应用。我什至只放了className={styles.customCss},但它不起作用
    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多