AnA*_*ice 6 css sass reactjs create-react-app
我正在为我的应用程序使用facebook 的 create-react 应用程序:
在我的 Login.js 容器中,我像这样导入 CSS:
import React from 'react';
import '../../styles/users/Login.css'
const Login = () => {
....
Run Code Online (Sandbox Code Playgroud)
问题是 Login.css 样式被应用于我的整个应用程序......例如,如果 Login.css 有:
body {
background:Red ;
}
Run Code Online (Sandbox Code Playgroud)
整个应用程序将有一个背景主体:红色;即使在登录容器之外。
我期望/想要的是容器内的 CSS 导入仅适用于该特定容器。
React 可能吗?React 开发人员应该如何处理容器特定的样式?我是否需要为所有容器添加一个 ID 并将其包含在整个 CSS 文件中?
JS:
// in Link.js
import React from 'react';
import '../../styles/Link.css'
const Link = ({children, href}) => (
<a className="link" href={href}>{children}</a>
);
Run Code Online (Sandbox Code Playgroud)
CSS:
// Link.css
.link {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
JS:
// in Link.js
import React from 'react';
import '../../styles/Link.css'
const Link = ({children, href}) => (
<a style={color: 'red'} href={href}>{children}</a>
);
Run Code Online (Sandbox Code Playgroud)
有一些库试图解决样式问题:
观看此演讲:https://speakerdeck.com/vjeux/react-css-in-js
看看这个:https: //github.com/cssinjs
样式组件: https: //github.com/styled-components/styled-components
| 归档时间: |
|
| 查看次数: |
3797 次 |
| 最近记录: |