reactjs - 在 makeStyles 中引用样式而不是类名连接
问题描述
我经常遇到这样的情况,我想重新使用样式定义来获得类似的效果。
假设我想要一个image
andbutton
具有相同的hover
效果,我会定义这样的样式:
const useStyles = makeStyles({
hoverable: {
transition: 'all .2s ease-in-out',
"&:hover": {
transform: 'scale(1.29)',
},
},
img: {
borderRadius: '10px',
},
btn: {
color: 'white',
},
});
css
classes
然后通过连接之前定义并通过useStyles
-hook生成的所需组件在组件上使用它们:
const classes = useStyles();
<img className={`${classes.img} ${classes.hoverable}`} src={someImg} alt={someImg} />
<button className={`${classes.btn} ${classes.hoverable}`}>Ok<button />
虽然这很好用,但我最终在样式定义和组件本身之间来回跳跃,以了解应用哪些样式以及有时需要多个连接,所以这就是我想做的事情:
import {CreateCSSProperties, PropsFunc} from '@material-ui/styles/withStyles'
const hoverable: React.CSSProperties | CreateCSSProperties<{}> | PropsFunc<{}, CreateCSSProperties<{}>> = {
transition: 'all .2s ease-in-out',
"&:hover": {
transform: 'scale(1.29)',
},
}
const useStyles = makeStyles({
img: {
borderRadius: '10px',
...hoverable
},
btn: {
color: 'white',
...hoverable
},
});
当然,可以type
为上面很长的类型串联创建一个方便的包装器 `alias,并将其导入到需要的任何地方。
由于我还没有在任何地方看到此解决方案,我想知道是否有任何理由不使用它,因为与通过class
-name 连接的常见样式组合相比,它在某些情况下可能是不利的?
解决方案
推荐阅读
- protocol-buffers - AIP GET 是否需要有请求消息
- asp.net - 从另一个 aspx 页面关闭 ModalPopupExtender 控件
- azure - Azure API 管理。如何添加 URL 路径过滤
- python - 以前的 WebDriver.get() 应该如何等待以后的 WebDriverWait()?
- reactjs - 如何从客户端重试graphQl查询
- java - JAVA用抽象方法覆盖接口方法
- amazon-web-services - AWS Kinesis Lambda DynamoDB 。我正在尝试使用以下代码通过 Lambda 函数从 Kinesis 摄取数据 DynamoDB
- java - 在弹性搜索中的单个翻转动作中翻转多个别名?
- c++ - 如何编写一个将运算符作为参数并具有默认值的函数?
- python - 如何使用带有 python-flask 的 swagger yaml 将 apikey 添加到 API(创建自己的 API)