reactjs - 在反应中通过主题提供者的材料 UI 多种字体
问题描述
是否可以选择在主题提供程序中提供多种字体以做出反应,而不是通过钩子或其他方式动态地将其中一种字体应用于不同的元素/组件?
BR,伊戈尔
解决方案
是的。您可以通过主题配置提供多种字体。
const theme = createMuiTheme({
typography: {
a: {
fontFamily: "Roboto",
fontSize: 12
},
b: {
fontFamily: "Helvetica",
fontSize: 14
}
}
});
<ThemeProvider theme={theme}>
<... />
</ThemeProvider>
在你的组件中:
const useStyles = makeStyles((theme) => ({
textA: {
...theme.typography.a,
},
textB: {
...theme.typography.b,
}
}));
const Greeting = ({useA}) => {
const classes = useStyles();
return <p className={useA ? classes.textA : classes.textB}>Hey</p>;
};
如果您不想创建两个类名,您也可以将属性传递给useStyles
并根据此属性设置类内的字体。
推荐阅读
- reactjs - 他为什么先
列表中的元素没有显示其他使用 React Bootstrap 的机器人? - c# - 在多个项目中为连接字符串使用外部配置文件
- java - 如何将数组列表中的值从 AsyncTask 类发送到 Android 中的另一个类?
- javascript - Angular ReferenceError:从 html 表单或按钮调用 typescript 函数
- android - 在android studio中打开新项目时android资源编译失败
- r - 按唯一标识符折叠多行,多个值(列),每个变量的多个元素(行)
- jquery - 获取每个 .class 的编号
- c++ - 为什么按值传递局部变量有效?
- c# - XPathNavigator 可以从测试输入中选择,但不能从真实输入中选择
- c# - C# - 使用 String.Split(CharArray) 时识别匹配字符