简体   繁体   English

减少样式以响应组件

[英]Isolate less styles to react component

I am trying to figure out how to get my react components to not pick up the css classes of another react component. 我试图弄清楚如何让我的React组件不占用另一个React组件的CSS类。 So say I have a component called Home and one called About, and on the inside they both just contain a div with a className of header-container and their less imports like so: 可以这么说,我有一个名为Home的组件,一个名为About的组件,并且在内部,它们都只包含一个div,其头文件容器的className为:

Home: 家:

import React, { Component } from 'react';

import './home.less';

class Home extends Component {
    render() {
        return (
            <div className='header-container'>
                <h1>Home</h1>
            </div>
        );
    }
}

export default Home;

About: 关于:

import React, { Component } from 'react';

import './about.less';

class About extends Component {
    render() {
        return (
            <div className='header-container'>
                <h1>About</h1>
            </div>
        );
    }
}

export default About;

And the less file for each looks like so with different colors (For this example lets say this is the about.less file): 每个文件的较少文件看起来都具有不同的颜色(对于本示例,可以说这是about.less文件):

.header-container {
    color: red;
}

Now when I use the react-router-dom and try to navigate to the home page, I am seeing the word Home but with the red color that is in the about less file, instead of the blue in my home.less file. 现在,当我使用react-router-dom并尝试导航到主页时,我看到的是Home字样,但文件中的红色大约是红色,而不是home.less文件中的蓝色。

This is how I am navigating to the components 这就是我导航到组件的方式

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';

import Home from './home/home';
import About from './about/about';

class App extends Component {
    render() {
        return (
            <Router>
                <Switch>
                    <Route exact path='/' component={Home}/>
                    <Route path='/about' component={About}/>
                </Switch>
            </Router>
        );
    }
}

export default App;

Anybody have any suggestions on how I can seperate these to not affect eachother? 有人对我如何分开这些建议不互相影响有任何建议吗? Not sure if it helps, but when I am seeing this I am running webpack-dev-server to serve the files. 不确定是否有帮助,但是当我看到此消息时,我正在运行webpack-dev-server来提供文件。

Edit: Also I noticed when I inspect element on the page loaded, I am seeing the following css in my head element. 编辑:当我检查加载页面上的元素时,我也注意到,我在head元素中看到以下CSS。

在此处输入图片说明

If it's going to be in the same app and you want to use an external style sheet, it's recommended that you name the two div s differently. 如果要在同一个应用程序中,并且要使用外部样式表,建议对两个div命名。

eg <div className='about-page__header-container'> <div className='home-page__header-container'> 例如<div className='about-page__header-container'> <div className='home-page__header-container'>

This is because the css styles imported as the style sheet are global. 这是因为作为样式表导入的css样式是全局的。

An alternative would be to use inline style, which is becoming increasingly popular in React.js community. 一种替代方法是使用内联样式,该样式在React.js社区中越来越流行。

For a comparison of the two options, see The Debate Around "Do We Even Need CSS Anymore?" 有关两个选项的比较,请参见“我们是否还需要CSS?”周围的辩论。

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

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