简体   繁体   English

用class样式化React Component

[英]Styling React Component with class

How can we define CSS class within react component, not in external style sheet? 我们如何在React组件而不是外部样式表中定义CSS类? I know there is a concept call CSS module which solve the issue of global style sheet. 我知道有一个概念调用CSS模块可以解决全局样式表的问题。 Is there any other way to style component by defining classes with in component? 还有其他方法可以通过在in组件中定义类来为组件设置样式?

在此处输入图片说明 it's very simple, just run command "npm run eject" It moves create-react-app's configuration files and dev/build/test scripts into you app directory. 这非常简单,只需运行命令“ npm run弹出”即可将create-react-app的配置文件和dev / build / test脚本移动到您的应用目录中。

now go to config folder and open 'webPackConfig.js' file and add the following code 现在转到config文件夹并打开'webPackConfig.js'文件并添加以下代码

          use: getStyleLoaders({
              ....
              ....
            modules:true,
            getLocalIdent:getCSSModuleLocalIdent,
            localIdentName: '[name]_[local]_[hash:base64:5]'
          })

example: 例:

import React from 'xyz';
import Logo from '../../Logo/Logo';

import classes from './Toolbar.css';
import NavigationItems from '../NavigationItems/NavigationItems';
import DrawerToggle from '../SideDrawer/DrawerToggle/DrawerToggle';

const toolbar = (props) => (
    <header className={classes.Toolbar}>
        <DrawerToggle clicked={props.DrawerToggleClicked}/>
        <div className={classes.Logo}>
            <Logo />
        </div>
        <nav className={classes.DesktopOnly}>
            <NavigationItems />
        </nav>
    </header>
);
export default toolbar;

try this and you can definitely solve your issue 试试这个,你绝对可以解决你的问题

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

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