简体   繁体   English

使用CSS和ReactJS和Redux将CSS添加到1页

[英]Add css to 1 page using scss and ReactJS and redux

I am using ReactJS with redux. 我正在使用带有Redux的ReactJS。 I using scss. 我用的是scss。 lets say my path is: http://localhost:3000/login I need to add to this page: html:{ overflow:hidden} 可以说我的路径是: http:// localhost:3000 / login我需要添加到此页面:html:{overflow:hidden}

and on other pages i want to remove this attribute. 在其他页面上,我想删除此属性。

Anyone have a clue? 有人知道吗?

I don't know how is your project architecture, but you can add a class (className) into your HTML tag in differently ways. 我不知道您的项目架构如何,但是您可以通过不同的方式将类(className)添加到HTML标记中。

If you want, you can also use your redux state. 如果需要,还可以使用redux状态。 You check if you are in X page, if it's ok, pass a boolean at true and if it's true, put your css. 您检查是否在X页面中,如果还可以,则将布尔值传递为true,如果为true,则将CSS放进去。

I prefer the first solution. 我更喜欢第一个解决方案。

You can change the style attribute of the html tag: 您可以更改html标记的style属性:

class MyPage extends React.Component {

  componentWillMount() {
    this.htmlTag = document.getElementsByTagName('html')[0];
    this.htmlTag.setAttribute('style', 'overflow: hidden');  
  }

  componentWillUnmount() {
    this.htmlTag.setAttribute('style', '');
  }

  ...
}

You could just import a className, let's say loginStyle , and make it so: 您可以只导入一个className,比如loginStyle ,并这样:

html: { overflow: hidden; }

Then, you just put it as a condition let's say on your header (has to be an element present in every page). 然后,只需将其作为条件放在标题上即可(必须是每个页面中都存在的元素)。

Something like 就像是

const isLogin = window.location.pathname === login ? true : false const isLogin = window.location.pathname === login ? true : false ( <= this is not the real condition, but make it so isLogin equals true is you are on your login page). const isLogin = window.location.pathname === login ? true : false (<=这不是真实条件,但要确保isLogin等于true, isLogin是您位于登录页面上)。

<Header className={ ${className1} ${className2} ${isLogin ? <Header className={ $ {className1} $ {className2} $ {isLogin吗? loginStyle : ' '} }/> loginStyle:''} }/>

And your style will only be applied on your login page. 并且您的样式将仅应用于您的登录页面。 Maybe not the simpliest, but at least this would work :) 也许不是最简单的方法,但是至少这可以工作:)

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

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