首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用@emotion/styled访问样式化组件中的主题

无法使用@emotion/styled访问样式化组件中的主题
EN

Stack Overflow用户
提问于 2021-10-26 06:38:58
回答 1查看 543关注 0票数 3

我使用@emotion/react主题化和注入主题到它,我能够访问主题使用useTheme到组件,但不能访问主题到样式使用@emotion/styled。有什么需要帮忙的吗?

代码语言:javascript
复制
//libs
import React from 'react';
import styled from '@emotion/styled';

import StylesProvider from '@mui/styles/StylesProvider';
import { ThemeProvider } from '@emotion/react';


const THEME = {
  primary: 'red'
}

const StyledText = styled('p')`
  color: ${({ theme }) => theme.primary};
`;

function App() {
  return (
    <StylesProvider injectFirst>
      <ThemeProvider theme={THEME}>
        <StyledText>test</StyledText>
      </ThemeProvider>
    </StylesProvider>
  );
}

export default App;



here is sample code
EN

回答 1

Stack Overflow用户

发布于 2021-10-26 06:40:48

你需要这样叫它。有关更多详细信息,请参阅this部分。

代码语言:javascript
复制
const StyledTextField = styled('div')(({ theme }) => `
  margin: ${theme.spacing(1)};
  background-color: ${theme.palette.primary.main};
  /* ... */
`);

或者,您也可以像在文档中那样使用JS对象:

代码语言:javascript
复制
const StyledTextField = styled('div')(({ theme }) => ({
  margin: theme.spacing(1),
  backgroundColor: theme.palette.primary.main,
  // ...
}));

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69718472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档