百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>CSS教程> react中运用css的七种办法介绍(附代码)
分享文章到:

react中运用css的七种办法介绍(附代码)

发布时间:09/01 来源:未知 浏览: 关键词:
本篇文章给大家带来的内容是关于react中使用css的七种办法介绍(附代码),有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

第一种: 在组件中直接使用style

不需要组件从外部引入css文件,直接在组件中书写。

import React, { Component } from "react";

const div1 = {
  width: "300px",
  margin: "30px auto",
  backgroundColor: "#44014C",  //驼峰法
  minHeight: "200px",
  boxSizing: "border-box"
};

class Test extends Component {
  constructor(props, context) {
    super(props);
  }
 
  render() {
    return (
     <div>
       <div style={div1}>123</div>
       <div style={{backgroundColor:"red"}}>
     </div>
    );
  }
}

export default Test;

留意事项:
1.在正常的css中,比方background-color,box-sizing等属性,在style对象p1中的属性中,必需转换成驼峰法,backgroundColor,boxSizing。而没有连字符的属性,如margin,width等,则在style对象中不变。
2.在正常的css中,css的值不需要用双引好(""),如

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

3.不克不及直接使用字符串写style,会报错

<div style="background-color:red">

484027244-5c668bdb49e62_articlex.png

而在react中使用style对象的方式时。值必需用双引号包裹起来。

这种方式的react样式,只作用于当前组件。

第二种: 在组件中引入[name].css文件

需要在当前组件开头使用import引入css文件。

import React, { Component } from "react";
import TestChidren from "./TestChidren";
import "@/assets/css/index.css";

class Test extends Component {
  constructor(props, context) {
    super(props);
  }
 
  render() {
    return (
      <div>
        <div className="link-name">123</div>
        <TestChidren>测试子组件的样式</TestChidren>
      </div>

    );
  }
}

export default Test;

这种方式引入的css样式,会作用于当前组件及其所有后代组件。

第三种: 3、在组件中引入[name].scss文件

react内部已经支撑了后缀为scss的文件,所以只需要安置node-sass即可,由于有了node-sass,scss文件才能在node环境上编译成css文件。

>yarn add node-sass

然后编写scss文件

//index.scss
.App{
  background-color: #282c34;
  .header{
    min-height: 100vh;
    color: white;
  }
}

关于怎样具体的使用sass,请查看sass官网

这种方式引入的css样式,一样会作用于当前组件及其所有后代组件

第四种: 在组件中引入[name].module.css文件

将css文件作为一个模块引入,这个模块中的所有css,只作用于当前组件。不会影响当前组件的后代组件。

import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.css";

class Test extends Component {
  constructor(props, context) {
    super(props);
  }  
 
  render() {
    return (
     <div>
       <div className={moduleCss.linkName}>321321</div>
       <TestChild></TestChild>
     </div>
    );
  }
}

export default Test;

这种方式可以看做是前面第一种在组件中使用style的升级版。完全将css和组件别离开,又不会影响其他组件。

第五种: 在组件中引入 [name].module.scss文件

相似于第四种,不同是第四种引入css module,而这种是引入 scss module罢了。

import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.scss";

class Test extends Component {
  constructor(props, context) {
    super(props);
  }  
 
  render() {
    return (
     <div>
       <div className={moduleCss.linkName}>321321</div>
       <TestChild></TestChild>
     </div>
    );
  }
}

export default Test;

一样这种方式可以看做是前面第一种在组件中使用style的升级版。

第六种: 使用styled-components

需要先安置

>yarn add styled-components

然后创立一个js文件(留意是js文件,不是css文件)

//style.js
import styled, { createGlobalStyle } from "styled-components";

export const SelfLink = styled.p`
  height: 50px;
  border: 1px solid red;
  color: yellow;
`;

export const SelfButton = styled.p`
  height: 150px;
  width: 150px;
  color: ${props => props.color};
  background-image: url(${props => props.src});
  background-size: 150px 150px;
`;

组件中使用styled-components样式

import React, { Component } from "react";

import { SelfLink, SelfButton } from "./style";

class Test extends Component {
  constructor(props, context) {
    super(props);
  }  
 
  render() {
    return (
     <div>
       <SelfLink title="People's Republic of China">app.js</SelfLink>
       <SelfButton color="palevioletred" style={{ color: "pink" }} src={fist}>
          SelfButton
        </SelfButton>
     </div>
    );
  }
}

export default Test;

这种方式是将整个css样式,和html节点团体合并成一个组件。引入这个组件html和css都有了。
它的好处在于可以随时通过往组件上传入 属性,来动态的改动样式。关于处置变量、媒体查询、伪类等较利便的。

这种方式的css也只对当前组件有效。

详细用途,请查看styled-components官网

第七种: 使用radium

需要先安置

>yarn add radium

然后在react组件中直接引入使用

import React, { Component } from "react";
import Radium from 'radium';

let styles = {
  base: {
    color: '#fff',
    ':hover': {
      background: '#0074d9'
    }
  },
  primary: {
    background: '#0074D9'
  },
  warning: {
    background: '#FF4136'
  }
};

class Test extends Component {
  constructor(props, context) {
    super(props);
  }  
 
  render() {
    return (
     <div>
      <button style={[ styles.base, styles.primary ]}>
        this is a primary button
      </button>
     </div>
    );
  }
}

export default Radium(Test);

关于处置变量、媒体查询、伪类等是不利便的。

使用Radium可以直接处置变量、媒体查询、伪类等,并且可以直接使用js中的数学,连接,正则表达式,前提,函数等。

详细用途请查看radium官网

留意:在export此前,必需用Radium包裹。

本篇文章到这里就已经全部完毕了,更多其他出色内容可以关注PHP中文网的CSS视频教程栏目!

以上就是react中使用css的七种办法介绍(附代码)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有153人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板