reactjs - 从也可以导入的样式组件中创建全局样式的替代方法
问题描述
我们目前正在加载字体和其他一些像这样的全局样式:
import { createGlobalStyle } from 'styled-components';
export default createGlobalStyle`
@font-face {
font-family: 'Name';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/name/v12/iJWZBXyIfDnIV5PNhY1KTN7Z-Yh-B4iFU0U1Z4Y.woff2) format('woff2');
}
// more fonts..
}
在每个_app.tsx
(来自回购中的每个项目)中,我们只是
import GlobalStyle from @our-company/ui;
// few other imports
const AppProviders = ({ children, messages, locale }: Props): JSX.Element => {
return (
<IntlProvider
locale={locale || 'en-GB'}
key={locale}
messages={messages[locale]}
defaultLocale="en-GB"
>
<GlobalStyle />
<DsThemeProvider
locale={locale}
>
{children}
</DsThemeProvider>
</IntlProvider>
);
};
但是我们注意到GlobalStyle
在单击复选框元素时会导致不必要的字体重新加载(尝试将其放在 .css 中并加载它,然后再也不会发生)。
知道如何在GlobalStyle
不使用样式组件的情况下将这些样式导出为名称,这样我们就不必更改import
项目中所有应用程序的所有内容?
解决方案
为什么您不创建 main.css 并将其导入您的 index.css 或 app.css ,它的下载和缓存在用户浏览器上,因此您不再需要使用全局样式的组件
推荐阅读
- python - 如何找到使用 ssh 和 rsync 的方法
- c++ - 不知何故告诉编译器“不要处理代码行”
- java - 使用 SharedPreferences 创建多个用户
- r - 从嵌套循环返回完整列表
- mongodb - MongoDB 对大型多更新操作的可扩展性问题
- python - 将 python WeakSet 提供给列表构造函数是否安全?
- c - 如何打印出每个目录但作为路径名?(在身体描述中解释)
- spring - spring boot rest webservice,如何改进干净的代码?
- haskell - 我正在尝试在 haskell 中重新定义产品定义。我收到一条错误消息,提示“功能中的非详尽模式”
- javascript - 纱线:$未定义