我们想用useTheme
from @emotion/react
在库内和我们的应用程序内
我们正在使用:
-反应本机网络
-nx monorepo
-反应本机
这是我们的结构
-apps
--web (ReactJS)
--mobile (React-native)
-libs
--shared-ui
--components
--theming
当我们想在应用程序中使用 useTheme 时,我们必须像这样声明 Theme
import '@emotion/react';
import { ThemeType } from '@awesomeapp-frontend/ui';
declare module '@emotion/react' {
export interface Theme extends ThemeType {}
}
然后将其放入提供者中:
import { light } from '@awesomeapp-frontend/ui';
...
root.render(
<StrictMode>
<ThemeProvider theme={light}>
但此声明仅适用于 Web 应用程序,我们现在可以使用 ThemeType 的定义
.... // type for theme
export type ThemeType = {
colors: {
primary: string;
myOwnColor: string;
};
};
.... // in component
const App = () => {
const theme = useTheme();
console.log('THEME IN COMPONENT: ', theme);
return <div style={{ color: theme.colors.primary }}>Oi, I am web app</div>;
...
问题是我们如何使用这种类型ThemeProvider
在lib文件夹中
当我注销时 useTheme 数据就在那里
{
colors: {
primary: 'red'
myOwnColor: 'blue'
}
}
但打字稿不知道这一点并抱怨...我如何设置全局类型以使情感也可以在 lib 文件夹中使用?