资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在React中调用外部样式-创新互联

如何在React中调用外部样式?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的盐湖网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

一、关于css-in-js的认识

1、css-in-js是一种使用 js 编写 css 样式的 css 处理方案。它的实现方案有很多,比如styled-components、polished、glamorous(paypal 开源的,不再维护)、radium、emotion等等。

2、其中最成熟的便是styled-components和emotion。它们真正意义上实现了组件化style,可以说是专门为 react 打造的。

二、styled-components 简介

styled-components是 css-in-js 主流的实现方案,同时也是组件化style的主流实现方案。

下面是styled-components的一些特性:

1、唯一class类名:和 css-module 异曲同工,生成唯一类名,避免重复和全局污染,也不需要你费脑筋思考该如何命名。

2、无冗余css代码:它的样式和组件绑定,组件调用则样式起作用。意味着你不需要关心如何检测和删除那些未使用的 css 代码。

3、动态样式: 它可以很简单地调整和拓展组件的样式,而不需要建立很多个 class 类来维护组件的样式。

4、自动添加兼容前缀:和 Autoprefixer 类似,会自动添加浏览器兼容前缀以支持旧版浏览器。

5、支持变量和继承:你可以使用变量来设置不同的样式,使用这些不同样式时只需要给样式组件传递一个参数即可。

三、styled-components使用方式

1、安装

npm install styled-components

2、使用

styled-components主要基于 es6 的标签模板语法调用标签函数

import React, { Component } from 'react'
import styled from 'styled-components'

export default class Style extends Component {
 render() {
  return (
   <>
    
     我是标题     
      )  } } // 使用es6的模板字符串的方式(下面表示定义了h2的样式) const Title = styled.h2`  font-size: 20px;  color: #f00;

3、嵌套的使用

import React, { Component } from 'react'
import styled from 'styled-components'

export default class Style extends Component {
 render() {
  return (
   <>
    
            

你好

      我是内容
          
      )  } } const Content = styled.div`  width: 100%;  height: 500px;  border: 1px solid #f00;  > h3 {   color: pink;  }  > .content {   text-align: center;   color: #f00;  } `

4、使用props传递参数的方式

import React, { Component } from 'react'
import styled, { css } from 'styled-components'

export default class Style2 extends Component {
 render() {
  return (
   
          提交     
  )  } } const Button = styled.button`  font-size: 1em;  margin: 1em;  padding: 0.25em 1em;  border-radius: 5px;  color: palevioletred;  border: 2px solid palevioletred;  cursor: pointer;  ${props =>   props.primary &&   css`    border: 2px solid mediumseagreen;    color: mediumseagreen;   `} `

5、样式的继承

import React, { Component } from 'react'
import styled from 'styled-components'

export default class Style3 extends Component {
 render() {
  return (
   
          提交     
  )  } } const Button = styled.button`  background: palevioletred;  color: white; ` const ExtendingButton = styled(Button)`  font-size: 18px;  padding: 5px 25px; `

四、使用sass

使用create-react-app创建的项目是支持sass的但是需要自己安装

1、安装

npm install node-sass

2、直接使用

import React, { Component } from 'react'
import './style4.scss'

export default class Style4 extends Component {
 render() {
  return (
   
    我是标题
   
  )  } }

五、使用css-module

使用create-react-app创建的项目,默认情况下就支持css-module

1、样式文件必须以[name].module.css或[name].module.scss的形式命名

2、以变量的形式导入样式文件,比如 import styles from './style.module.css';

3、className以变量引用的方式添加,比如 className={ styles.title }

import React, { Component } from 'react'
import styles from './Style5.module.scss'

export default class Style5 extends Component {
 render() {
  return (
   
    我是标题
   
  )  } }

4、如果不想使用默认的哈希值

:global(.wrap) {
 color: green;
}
// 直接使用
你好

5、样式的继承

.className {
 color: green;
 background: red;
}

.otherClassName {
 composes: className; // 直接继承上面的
 color: yellow;
}

.title {
 composes: className from './another.css'; // 直接使用外部样式表
 color: red;
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联成都网站设计公司行业资讯频道,感谢您对创新互联成都网站设计公司的支持。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:如何在React中调用外部样式-创新互联
网站网址:http://cdkjz.cn/article/ddhdjo.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220