Apu*_*rvG 7 css stylus sass reactjs create-react-app
我在create-react-app上构建我的react应用程序.我希望每个组件都有本地css.我正在计划使用css模块但这些都是问题
如果我使用css模块
这是我的scss文件之一
$scalel: 1.7vmin;
.navbar-side {
position: absolute;
background: rgba(255,255,255,0.15);
width: 17 * $scalel;
top: 6.2 * $scalel;
padding-bottom: 2 * $scalel;
.tessact-logo {
width: 50%;
height: 12 * $scalel;
background: url('/public/images/logo.png');
background-repeat: no-repeat;
background-position: center center;
background-size: contain;
margin-left: 25%;
}
.navbar-item {
padding: 0.8 * $scalel;
transition: all 0.2s ease;
cursor: pointer;
padding-left: 3 * $scalel;
&:hover {
background: rgba(255,255,255,0.15);
}
&.active {
background: rgba(255,255,255,0.25);
}
.navbar-item-inside {
display: none;
}
}
}
Run Code Online (Sandbox Code Playgroud)
*How can use classname={style.navbar-item}? key is navbar-item is in nest.*
另外,如果我不想使用css模块,有什么办法吗?在其中一个项目中,我看到有人像这样隔离了css
import c from './Reviews.styl'
<div className={c.container}>
<ReviewSearch
assignIsOpen={this.state.assignIsOpen}
toggleAssign={this.toggleAssign}
selectedRows={this.props.selectedRows}
onSubmitProcess={this.onSubmitProcess}/>
<ReviewTable
isLoading={this.props.isLoading}
items={this.props.list}
selectedRows={this.props.selectedRows}
onRowSelection={this.onRowSelection}
authToken={this.props.auth_token}
setCurrentItem={this.setCurrentItem}/>
</div>
Run Code Online (Sandbox Code Playgroud)
样式文件是
:local(.container)
display block
width 100%
.control-container
display flex
align-items center
.control-label
width 120px
.control
flex 1
& + .control-container
margin-top 30px
Run Code Online (Sandbox Code Playgroud)
使用styl文件而不是css.我喜欢这种方式,因为在这里不需要在所有子div中使用style.classname.我怎样才能做到这一点?
自从提出这个问题以来,已经发生了很多变化。Create React App 有一段时间(因为react-scripts@2.0.0)支持 css 模块和scss开箱即用,无需弹出。
https://create-react-app.dev/docs/adding-a-css-modules-stylesheet
所有您需要做的是重新命名的任何文件somestyle.css,以somestyle.module.css它现在将表现为一个css模块。
您可以通过重命名someStyle.scss以相同的方式使用 scsssomestyle.module.scss
我建议使用类名。它是一个库,您可以通过它从 css 文件将 css 导入到组件中,然后按如下所示使用它:
import styles from './your.css'
import cn from 'classnames/bind'
const cx = cn.bind(styles)
<div className={cx('myDiv')}/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1407 次 |
| 最近记录: |