reactjs - Material UI - 将道具应用于伪元素
问题描述
import React from "react";
import { Box } from "@material-ui/core";
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles({
box: {
'&::before': {
content: props => `${props.text}` // should be props.text from <Child /> (i.e. '111')
}
}
});
function Parent() {
return <Child text="111" />
}
function Child(props) {
const { text } = props;
const classes = useStyles({ text });
return <Box className={classes.box} />
}
我怎样才能在外面使用道具<Child />
?我已经尝试过数据属性,但也没有工作。似乎content
在伪元素中与其他属性有点不同
解决方案
您可以在 useStyles 调用中传递道具。
const classes = useStyles({text: 'asdf'})
您的样式定义中的用法是这样的
const useStyles = makeStyles({
box: {
'&::before': {
content: (props) => `${props.text}`
}
}
});
推荐阅读
- javascript - 内部 html 脚本 window.addEventListener / window.onload 无法编辑特定的 div
- c# - 为什么我在 asp.net mvc C# 中出现此错误
- html - 浏览器应该如何解释在 srcset 中出现两次的相同路径
- spring - SpringFrameWork 缺少方法“SetPackagesToScan()”
- python - Shapely "is_valid" 对无效的重叠多边形返回 True
- php - 在数据库值上使用 CSS - PHP 框架
- powershell - 如何在 Powershell 脚本中自动安装 nuget
- reactjs - react-hook-form 和 MUI FormControl
- javascript - 单击笑脸并放置在输入字段中
- sql - 是否可以在 COALESCE SQL Server 中使用 IF 语句