为什么在 React 组件中内联 CSS 不起作用?

huangapple go评论71阅读模式
英文:

Why inline CSS is not working on component in react?

问题

我正在使用React中的一个组件。

import style from "../../Style/Styles.module.scss";

const Anchor = (props) => {
return <a className={style["anchorTags"]} href={props.href}>{props.title}</a>;
};

export default Anchor;

我已经在我的SCSS文件中为这个组件编写了CSS。

.anchorTags {
border: 2px solid $Very-Light-Gray;
color: $Very-Light-Gray;
}


但是,当我在另一个组件中使用时,尝试在组件中编写内联CSS时,它不起作用。

import style from "../../Style/Styles.module.scss";
import Anchor from "./achortag";

<Anchor
style={{ paddingBlock: "0.7em", paddingInline: "2em" }}
href="/viewPlan"
title="VIEW PLANS"
/>


请分享您的建议,这样会起作用吗?
英文:

I'm using a component in react.

import style from &quot;../../Style/Styles.module.scss&quot;;


const Anchor = (props) =&gt; {
  return &lt;a className={style[&quot;anchorTags&quot;]} href={props.href}&gt;{props.title}&lt;/a&gt;;
};

export default Anchor;

where I have already written CSS for this component in my SCSS file.

.anchorTags {
    border: 2px solid $Very-Light-Gray;
    color: $Very-Light-Gray;
}

but when I'm writing inline css in a component when using this on another component it is not working.


import style from &quot;../../Style/Styles.module.scss&quot;;
import Anchor from &quot;./achortag&quot;;

&lt;Anchor
          style={{ paddingBlock: &quot;0.7em&quot;, paddingInline: &quot;2em&quot; }}
          href=&quot;/viewPlan&quot;
          title=&quot;VIEW PLANS&quot;
        /&gt;

Please share your suggestion on how this will work?

答案1

得分: 4

在锚点组件中:

const Anchor = (props) => {
  return (
    <a className={style.anchorTags} href="#" style={props.style}>
      {props.title}
    </a>
  );
};

export default Anchor;

或者使用扩展运算符,这将添加任何附加属性:

const Anchor = (props) => {
  return (
    <a className={style.anchorTags} href="#" {...props}>
      {props.title}
    </a>
  );
};

export default Anchor;
英文:

In Anchor component:

const Anchor = (props) =&gt; {
  return (
    &lt;a className={style.anchorTags} href=&quot;#&quot; style={props.style}&gt;
      {props.title}
    &lt;/a&gt;
  );
};

export default Anchor;

or use the spread operator, this will add any additional attributes:

const Anchor = (props) =&gt; {
  return (
    &lt;a className={style.anchorTags} href=&quot;#&quot; {...props}&gt;
      {props.title}
    &lt;/a&gt;
  );
};

export default Anchor;

答案2

得分: 3

使用所需的样式,您必须使用以下语法:

import style from "../../Style/Styles.module.scss";

const Anchor = (props) => (
  <a className={style.anchorTags} href={props.href} {...props}>{props.title}</a>
);

export default Anchor;

要了解更多信息,您可以阅读此文档:添加 CSS 模块样式表

英文:

To use the desired style, you must use this syntax:

import style from &quot;../../Style/Styles.module.scss&quot;;

const Anchor = (props) =&gt; (
  &lt;a className={style.anchorTags} href={props.href} {...props}&gt;{props.title}&lt;/a&gt;;
);

export default Anchor;

For more information, you can read this (Adding a CSS Modules Stylesheet).

huangapple
  • 本文由 发表于 2023年2月6日 19:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定