简体   繁体   English

如何通过 reactjs 在 css 文件中添加新的 css 属性

[英]How to add new css property in css file through reactjs

I think, it might be silly question to ask but trust me I have case where I want to insert new css property to existing css file ( which are already imported ).我认为,这可能是一个愚蠢的问题,但相信我,我想将新的 css 属性插入现有的 css 文件(已经导入)。 I tried very hard but didn't find any solution to resolve this solution.我非常努力,但没有找到解决此解决方案的任何解决方案。 Could someone please help me in this issue.有人可以帮我解决这个问题。

Thanks谢谢

This is how you can pass extra styles to the component separately.这就是您可以将额外的 styles 单独传递给组件的方式。 :

// react native example
<HomeScreen style={[styles.homeContainer, {width: 100, color: "red"}]}/>

here is how you can conditionally pass the specific classname as your specified in the comments section, and use that class name to set the custom styles:这是您如何有条件地传递您在评论部分中指定的特定类名,并使用该 class 名称来设置自定义 styles:

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [red, setRed] = useState(true);
  return (
    <div className={red ? "redClass" : "greenClass"}>
      <p>hello</p>
      <button onClick={() => setRed(!red)}>Change Color</button>
    </div>
  );
}

Here is how the css styles for redClass and greenClass looks:以下是 redClass 和 greenClass 的 css styles 的外观:

.redClass {
  width: 100px;
  height: 100px;
  background-color: red;
}

.greenClass {
  width: 100px;
  height: 100px;
  background-color: green;
}

body {
  background-color: aqua;
}

Codesandbox Example 代码沙盒示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM