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

Rup*_*ind 7 emotion typescript reactjs material-ui styled-components

在此输入图像描述

我正在使用@emotion/react主题并将主题注入其中,我可以使用useTheme组件访问主题,但无法使用@emotion/styled. 有什么帮助吗?

//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
Run Code Online (Sandbox Code Playgroud)

Nea*_*arl 1

你需要这样称呼它。请参阅节了解更多详细信息。

const StyledTextField = styled('div')(({ theme }) => `
  margin: ${theme.spacing(1)};
  background-color: ${theme.palette.primary.main};
  /* ... */
`);
Run Code Online (Sandbox Code Playgroud)

或者您也可以使用 JS 对象,如文档中所示:

const StyledTextField = styled('div')(({ theme }) => ({
  margin: theme.spacing(1),
  backgroundColor: theme.palette.primary.main,
  // ...
}));
Run Code Online (Sandbox Code Playgroud)

Codesandbox 演示