html - NextJs 将 prop 作为 className 传入
问题描述
我在将道具传递给 className 时遇到了麻烦,我想知道它是否可能。
例如,假设category
is 传递到函数中并且category
is "category1"
。在 className 中我可以调用styles.category
获取styles.category1
吗?
export const function ({category}) => {
return (
<button className={styles.category}></button>
)
}
.category1 {
background: #FF4646 0% 0% no-repeat padding-box;
opacity: 1;
width: 200px;
height: 200px;
outline: none;
border: none;
font: url('../../public/fonts/AirbnbCerealBlack.woff');
font-size: 20px;
font-weight:900;
color: black;
}
.category2 {
background: #4EEA9C 0% 0% no-repeat padding-box;
opacity: 1;
width: 270px;
height: 270px;
outline: none;
border: none;
font: url('../../public/fonts/AirbnbCerealBlack.woff');
font-size: 20px;
font-weight:900;
}
解决方案
你可以用它styles[category]
来设置className
你的按钮。
export const function ({ category }) =>{
return (
<button className={styles[category]}></button>
)
}
推荐阅读
- image - 如何在反应本机应用程序中通过相机(反应本机图像选择器)捕获和渲染多个图像
- javascript - 如何在 Spring MVC 的 JSP 登录页面中实现记住我?
- javascript - @keyup.delete输入直到为空时如何触发功能?VueJS
- django - 即使级别设置为 ERROR,Django 记录器的级别也不会跟踪 INFO 消息
- javascript - 无法在 VAST - FLUID PLAYER 中显示“跳过广告”
- node.js - 如何在通过 sql 查询保存数据或更新数据时删除脚本注入?
- entity-framework - EF Core 不能在父级中包含子级
- javascript - NodeJS:从 readStream 获取文件名
- bash - 正则表达式匹配以点结尾的所有内容
- fonts - Django xhtml2pdf 有孟加拉字体问题(损坏的格式)