在 React 中编写 CSS 样式的 5 种方法
最初发布于nordschool。
有很多方法可以设置 React 组件的样式。🤓
在本指南中,我们将介绍设置 React 组件样式的方法。我们将向您展示 5 种不同的方法,您可以选择最合适的一种!😁
按自己的方式设计组件
这些是我们将要讨论的组件样式方式:
我们将使用不同的组件样式方式替换下面相同的样式。
/* BeautifulButton.css */
.button {
color: #494949;
text-transform: uppercase;
text-decoration: none;
background: #ffffff;
padding: 20px;
font-size: 20px;
border: 4px solid #494949;
display: inline-block;
transition: all 0.4s ease 0s;
}
.button:hover {
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
transition: all 0.4s ease 0s;
}
.button--primary {
color: #ffffff;
text-transform: uppercase;
text-decoration: none;
background: #f6b93b;
padding: 20px;
font-size: 20px;
border: 4px solid #f6b93b;
display: inline-block;
transition: all 0.4s ease 0s;
}
.button--primary:hover {
color: #494949;
background: #ffffff;
border-color: #494949;
transition: all 0.4s ease 0s;
}
常规 CSS
这是向 React 组件添加样式的最简单、最直接的方法。
import React from 'react';
import './BeautifulButton.css';
const MyBeautifulButton = props => {
return (
<div>
<button className={props.primary ? 'button--primary' : 'button'}>
Button
</button>
</div>
);
};
export default MyBeautifulButton;
内联样式
import React from 'react';
const MyBeautifulButton = props => {
const button = {
color: '#494949',
textTransform: 'uppercase',
textDecoration: 'none',
background: '#ffffff',
padding: '20px',
fontSize: '20px',
border: '4px solid #494949',
display: 'inline-block',
transition: 'all 0.4s ease 0s'
};
const primaryButton = {
...button,
color: '#ffffff',
background: '#f6b93b',
borderColor: '#f6b93b'
};
return (
<div>
<button style={props.primary ? primaryButton : button}>Button</button>
</div>
);
};
export default MyBeautifulButton;
注意:内联样式必须使用驼峰命名法!例如borderColor。
内联样式很简单,开箱即用,但也有很多限制。
例如,没有直接的方法来添加悬停效果。
建议:仅对小型简单组件使用内联样式范例。😇
CSS 模块
import React from 'react';
import styles from './my-beautiful-button.module.css'; // must have extension .module.css.
// my-beautiful-button.module.css has the same styles as Button.css.
const MyBeautifulButton = props => {
return (
<div>
<button
className={props.primary ? styles['button--primary'] : styles.button}
>
Button
</button>
</div>
);
};
export default MyBeautifulButton;
CSS 模块中的所有样式都作用于导入它的组件。这意味着你不必担心全局名称冲突。😌
注意:名称中必须包含子扩展名 .module。否则,样式将按常规 CSS 加载,可能会出现名称冲突。
预处理器
在 React 中可以直接使用SCSS、LESS或Stylus等预处理器。
您需要添加预处理器加载器,然后以与常规 CSS 相同的方式导入文件。
import React from 'react';
import './button.scss'; // <-- Once webpack configs have the right loader this should work like regular scss.
const MyBeautifulButton = props => {
return (
<div>
<button className={props.primary ? 'button--primary' : 'button'}>
Button
</button>
</div>
);
};
export default MyBeautifulButton;
JS 中的 CSS
JS 中的 CSS 是一种模式,你可以在组件内部定义所有样式。类似内联样式,但功能更强大。
让我们看看如何使用Styled Components和Emotion编写相同的按钮样式。
样式化组件
import React from 'react';
import styled from 'styled-components';
const MyBeautifulButton = props => {
const BeautifulButton = styled.button`
color: #494949;
text-transform: uppercase;
text-decoration: none;
background: #ffffff;
padding: 20px;
font-size: 20px;
border: 4px solid #494949;
display: inline-block;
transition: all 0.4s ease 0s;
&:hover {
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
transition: all 0.4s ease 0s;
}
`;
const BeautifulPrimaryButton = styled(Button)`
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
&:hover {
color: #494949;
background: #ffffff;
border-color: #494949;
}
`;
return (
<div>
{props.primary ? (
<BeautifulPrimaryButton>Button </BeautifulPrimaryButton>
) : (
<BeautifulButton>Button</BeautifulButton>
)}
</div>
);
};
export default MyBeautifulButton;
情感
Emotion 有两种添加样式的方式,css API 和 styled API。
下面是使用 css API 的示例:
// this comment tells babel to convert jsx to calls to a function called jsx instead of React.createElement
/* @jsx jsx */
import React from 'react';
import { jsx, css } from '@emotion/core';
const BeautifulButton = css`
color: #494949;
text-transform: uppercase;
text-decoration: none;
background: #ffffff;
padding: 20px;
font-size: 20px;
border: 4px solid #494949;
display: inline-block;
transition: all 0.4s ease 0s;
&:hover {
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
transition: all 0.4s ease 0s;
}
`;
const BeautifulPrimaryButton = css`
${Button};
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
&:hover {
color: #494949;
background: #ffffff;
border-color: #494949;
}
`;
const MyBeautifulButton = props => {
return (
<div>
<button css={props.primary ? BeautifulPrimaryButton : BeautifulButton}>Button</button>
</div>
);
};
export default MyBeautifulButton;
Emotion 的样式化 API 与 Styled Components 非常相似。以下是示例:
// this comment tells babel to convert jsx to calls to a function called jsx instead of React.createElement
/* @jsx jsx */
import React from 'react';
import { jsx, css } from '@emotion/core';
import styled from '@emotion/styled';
const BeautifulButton = styled.button`
color: #494949;
text-transform: uppercase;
text-decoration: none;
background: #ffffff;
padding: 20px;
font-size: 20px;
border: 4px solid #494949;
display: inline-block;
transition: all 0.4s ease 0s;
&:hover {
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
transition: all 0.4s ease 0s;
}
`
const BeautifulPrimaryButton = styled(BeautifulButton)`
color: #ffffff;
background: #f6b93b;
border-color: #f6b93b;
&:hover {
color: #494949;
background: #ffffff;
border-color: #494949;
}
`
const MyBeautifulButton = (props) => {
return (
<div>
{props.primary ? <BeautifulPrimaryButton>Button </BeautifulPrimaryButton> : <BeautifulButton>Button</BeautifulButton>}
</div>
);
};
export default MyBeautifulButton;
注意:您可以添加babel 插件以避免编写所需文件的第一行。
/** @jsx jsx */
JS 中的 CSS 是一种强大的模式。在构建大型复杂的 Web 应用程序时,它使很多事情变得更容易。
Styled Components 和 Emotion 之间有一个主要区别。
Styled 组件仅提供样式 API。而 Emotion 提供了两种添加样式的方式:css和样式API。
建议:对于大型应用程序,我们建议使用 Emotion。👌
以下是其他值得一看的 JS 库中的 CSS:
现在你知道了组件样式的选项!你更喜欢哪一个?🤔
支持
喜欢这篇文章吗?请在 Twitter 上分享摘要。
更好的代码周一通讯
你可能也会喜欢我的新闻通讯。我的目标是每周一分享 3 个 Web 开发技巧。
我的目标是提升写作能力,并尽可能地分享知识。目前为止,已有数百名开发者订阅,他们似乎很喜欢。
鏂囩珷鏉ユ簮锛�https://dev.to/nordschool/5-ways-to-write-css-styles-in-react-3jo0