繁体   English   中英

!important inline styles in react

[英]!important inline styles in react

有没有一种方法可以使用?重要的覆盖来添加内联样式?

style={
  height: 20+'!important'
};

<div style={style}></div>

这不像我希望的那样有效。

显然 React 不支持这一点。 但是我在研究时得到了这个黑客

    <div ref={(node) => {
      if (node) {
        node.style.setProperty("float", "right", "important");
      }
    }}>                    
    </div>

祝你好运:)

20+'!important''20!important'

当你只给一个数字时,react 会为你添加“px”; 但是您使用的是字符串,因此您必须指定单位。 此外,我很确定“!important”和它左边的任何东西之间需要有一个空格。

style={{ height: '20px !important' }};

一个很好的使用技巧,它对其他 CSS 属性更清晰、更一致:

ref={(el) => el && el.style.setProperty(<property>, <value>, "important")}

希望这可以帮助!

这是我可以让它与 React 16 一起工作的唯一方法。

const id="unique_id"; 
<React.Fragment>
    <style>
      {`
#${id} {
  background-color: transparent !important;
}
      `}
    </style>
    <Frame id={id} />
</React.Fragment>

如果您有充分的理由使用!important ,我建议使用样式组件,因为样式道具不支持!important并且将来可能不会支持。

这是一个示例,我们在grid columns上覆盖 Semantic-UI 的padding 您实际上可以省略!important ,因为“ 提高特异性”就足够了。

const StyledColumn = styled.div(({size}) => ({className: `${size} wide column`})`
    &&&&& {
        padding-top: 0.3rem !important;
        padding-bottom: 0.3rem !important;
    }
`
<StyledColumn size="three"></StyledColumn>

&&&&&& <- 提高特异性。

是的,我发现这样做的方式是上面已经提到的:

const styles = (theme: any) => ({
    panelSize: {
        height: 480,
        width: 360,
    },
    progress: {
        height: '120px !important', 
        margin: theme.spacing.unit * 2,
        width: '120px !important'
    }
});

上面的丹尼尔最初回答了这个问题。 我只是想分享我的答案,因为我重构了他的答案以使用钩子。

  1. 定义 ref const ref = useRef(null);
  2. 将 ref 添加到您想要的节点(例如 div、table) <div ref={ref}></div>
  3. 在 useLayoutEffect 中添加这段代码ref.current.style.setProperty(<property>, <value>, "important")

请参阅下面的示例代码

import React, { useRef, useLayoutEffect } from "react";


const SampleComponent = () => {

   const ref = useRef(null);

   useLayoutEffect(() => {
    ref.current.style.setProperty("border-top", "0px", "important");
  }, []);

   return (
      <div ref={ref}>
         {/* additional codes here */}
      </div>
   )
}

笔记:

  • 属性为 CSS 格式(例如 "border-top" )

我尝试了上述关于宽度的建议,但无法使其正常工作。

这对我有用,我创建了 style.css 文件,并在我的情况下添加了 width: 100% !important 到一个类,然后将该文件导入到我的组件中,然后调用这个类,它就可以工作了。

材料Ui文档中提到了另一种更清晰的方法,可以与react一起使用以覆盖默认行为

1-为样式声明一个常量变量

const widthStyle = {
  width:10
}

2-将它与JSX一起用于内联样式

<div style={widthStyle}></div>

使用现代语法对代码进行了一些改进,以使代码更短:

<div ref={(node) => 
        node?.style.setProperty("float", "right", "important")
    }>                    
</div>

我通过设置 className 道具解决了它

.height {
    height: 20px !important;
}

className={ styles.height }

我能够通过做这样的事情来解决它:

style={
  height: `20px ${" !important"}`
};

<div style={style}></div>

我必须在“.important”之前添加空格,因为浏览器在运行程序时会不断删除该空格。

有没有一种方法可以使用!important覆盖添加内联样式?

style={
  height: 20+'!important'
};

<div style={style}></div>

这不是我希望的那样。

暂无
暂无

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

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