reactjs - 如何使用 React 和 Typescript 使用情感/样式
问题描述
我已经包装了我的根组件,ThemeProvider
从中@emotion/react
可以访问props.theme
:
const StyledDiv = styled.div`
background-color: ${(props) => props.theme.palette.primary.main};
`;
问题: props.theme
根据 TS 是一个空对象 - palette
Theme 类型上不存在(但是我传递给主题提供者的对象确实包含该属性)。如果我让 TS 忽略这一点,则代码有效。
文档涵盖了这一点,这似乎是一个简单的解决方法: 扩展 Emotion 主题类型
但是,我对文档的理解不够好,无法使Theme
类型props.theme
具有正确的属性。
非常感谢任何为我指明正确方向的帮助!
解决方案
添加此文件types.d.ts
import "@emotion/react";
import { IPalette } from "../your-palette";
declare module "@emotion/react" {
export interface Theme extends IPalette {}
}
推荐阅读
- dart - 函数内的final关键字
- typescript - 打字稿 - 动态对象分配?
- python - 查找从客户端选择的端口的最佳方法
- php - 开发者机器上的 Laravel Apache 两个或多个项目
- css - Vuetify 样式不适用于 Tailwind
- java - Java反编译器,在eclipse中打开jar可执行文件并查看源码
- python - AttributeError: 'NoneType' object has no attribute 'next' ,此错误发生在第 40 行
- python - 如何从两个列表中获取一个列表
- sql-server - 在 SSMS 复制数据库向导中,如何访问目标文件夹中的文件(屏幕截图)?
- c - 断点未设置,即使它停在这里