博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7种在React中使用CSS的方式
阅读量:4117 次
发布时间:2019-05-25

本文共 3954 字,大约阅读时间需要 13 分钟。

第一种:在组件中直接使用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 (     
123
); }}export default Test;

注意事项:

  1. 在正常的css中,某种背景色,box-sizing等属性,在style对象div1中的属性中,必须转换成驼峰法,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;}

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

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

第二种:在组件中约会[name] .css文件

需要在当前组件开头使用导入约会css文件。

import React, { Component } from "react";import TestChidren from "./TestChidren";import "@/assets/css/index.scss";class Test extends Component {  constructor(props, context) {    super(props);  }  render() {    return (      
123
测试子组件的样式
); }}export default Test;

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

第三种:在组件中约会[name] .scss文件

datereact内部已经支持了后缀为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 (     
321321
); }}export default Test;

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

第五种:在组件中约会[name] .module.scss文件

某种第四种,区别是第四种约会css模块,而这种是约会scss模块而已。

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 (     
321321
); }}export default Test;

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

第六种:使用styled-components

需要先安装

>yarn add styled-components

然后创建一个js文件(注意是js文件,不是css文件)

//style.jsimport styled, { createGlobalStyle } from "styled-components";export const SelfLink = styled.div`  height: 50px;  border: 1px solid red;  color: yellow;;export const SelfButton = styled.div`  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 (     
app.js
SelfButton
); }}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 (     
); }}export default Radium(Test);

对于处理变量,媒体查询,伪类等是不方便的。

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

具体用法请查看radium官网

注意:

在export之前,必须用Radium包裹。

本文完~

转载地址:http://unbpi.baihongyu.com/

你可能感兴趣的文章
非常不错 Hadoop 的HDFS (Hadoop集群(第8期)_HDFS初探之旅)
查看>>
Tomcat启动错误,端口占用
查看>>
laravel 修改api返回默认的异常处理
查看>>
高德坐标转换百度坐标 javascript
查看>>
tp5封装通用的修改某列值
查看>>
laravel控制器与模型名称不统一
查看>>
vue登录拦截
查看>>
npm配置淘宝镜像仓库以及electron镜像
查看>>
linux设置开机自启动脚本的最佳方式
查看>>
VUE SPA 单页面应用 微信oauth网页授权
查看>>
phpstorm 集成 xdebug 进行调试
查看>>
npm和node升级的正确方式
查看>>
laravel事务
查看>>
springcloud 连续请求 500
查看>>
vue复用新增和编辑表单
查看>>
Ubuntu 16.04 apt-get更换为国内阿里云源
查看>>
laravel部署到宝塔步骤
查看>>
小程序获取access_token
查看>>
navicat远程连接mysql数据库
查看>>
tp5令牌数据无效 解决方法
查看>>