简体   繁体   English

更改路线时如何在React组件的CSS样式之间切换?

[英]How to switch between CSS styles of React components when changing routes?

I have a React app which now has two routes (the main landing page and career page). 我有一个React应用程序,它现在有两条路线(主登陆页面和职业页面)。

I made one reusable JumbotronResponsive component and i am rendering that single component as child under JumbotronLanding component (for landing page) and as child under JumbotronCareer component (for career page). 我制作了一个可重用的JumbotronResponsive组件,并将单个组件渲染为JumbotronLanding组件(用于登录页面)下的子项,以及作为JumbotronCareer组件(用于职业页面)下的子项。

Like this 像这样

import React, { PureComponent } from 'react'
import { ResponsiveJumbotron } from '../../HOC';
import './jumbotronCareer.css';

export default class JumbotronCareer extends PureComponent {

  render() {
    return (
      <>
        <ResponsiveJumbotron/>
      </>
    )
  }
}


and this 和这个

import React, { PureComponent } from 'react';
import { ResponsiveJumbotron } from '../../HOC';
import './jumbotronLanding.css';

export default class JumbotronLanding extends PureComponent {

  render() {
    return (
      <>
        <ResponsiveJumbotron/>
      </>
    )
  }
}

As you can see those parent components have styles in which i want to style some ResponsiveJumbotron 's parts. 如您所见,那些父组件具有一些样式,在这些样式中,我想为某些ResponsiveJumbotron的零件设置样式。
For example i wish font-size of jumbotron in landing page be 70px and in career page be 45px . 例如,我希望jumbotron在着陆页上的字体大小为70px ,在职业页面上为45px
But when i switch between routes those styles are like in one CSS file. 但是,当我在路线之间切换时,这些样式就像在一个CSS文件中一样。
The question is 'how to separate them from each other?'. 问题是“如何将它们彼此分开?”。

You can add a className property and in your ResponsiveJumbotron use it like 您可以添加className属性,并在您的ResponsiveJumbotron中使用它,例如

const ResponsiveJumbotron = props => <div className={props.className} />;

[...] 

export default class JumbotronLanding extends PureComponent {

  render() {
    return (
      <>
        <ResponsiveJumbotron className="landing-page" />
      </>
    )
  }
}

and you can use that css class in your stylesheet 您可以在样式表中使用该CSS类

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

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