Ben*_*Ben 7 css environment-variables reactjs create-react-app
我使用https://github.com/facebookincubator/create-react-app创建了一个简单的 React 应用程序。
我试图为组件的 CSS 指定一个可配置的 URL,如下所示:
.Cell {
background-image: url({process.env.REACT_APP_STATIC_URL}./someImage.png);
}
Run Code Online (Sandbox Code Playgroud)
这是组件:
import React from 'react';
import './Cell.css'
class Cell extends React.Component {
render() {
return (
<div className="Cell">
{this.props.name}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
但流程变量看起来并没有渗透到导入的 CSS 中。我该怎么做呢?
如果你想在 CSS 中使用 js 变量,请尝试使用 React inline-style 而不是普通的 css 文件。
https://facebook.github.io/react/tips/inline-styles.html
如果你想分离 CSS 和 JS,你可能需要一个 CSS 预处理器来管理你的全局变量。
| 归档时间: |
|
| 查看次数: |
7168 次 |
| 最近记录: |