gpt4 book ai didi

javascript - material ui 'new' v5.0.0 injectionFirst 无法设置特异性

转载 作者:行者123 更新时间:2023-12-05 00:28:45 27 4
gpt4 key购买 nike

v5.0.0今天发布了,在我的穗分支中,我正在尝试更新到新版本。我正在完成 mui-docs 详细 here 中的步骤。 .我正在使用 styled-componentsNextJS似乎 mui 样式是 不是 先注入(inject)。我不确定我可以采取哪些不同的措施来领先一步,afaik 我一直在正确地遵循该指南中的所有内容并逐步进行更改。然而,我仍然有特异性错误。
样式组件的样式低于其他所有内容。
enter image description here
<StylesProvider injectFirst>现在似乎无法正常工作
想法?
_app.tsx

import styled, { createGlobalStyle, ThemeProvider } from 'styled-components';
import withDarkMode, { useDarkMode } from 'next-dark-mode';
import defaultTheme from 'src/theme';
import TopNavigation from '@components/_molecules/TopNavigation/TopNavigation';
import StyledTypography from '@components/_atoms/Typography/StyledTypography';
import { Provider } from 'react-redux';
import { StylesProvider } from '@mui/styles';

function MyApp({ Component, pageProps }): JSX.Element {

// ? https://nextjs.org/docs/basic-features/layouts
const getLayout = Component.getLayout || (page => page);

return (
<StylesProvider injectFirst>
<ThemeProvider theme={setTheme}>
<RootStyles>
<PageLayout>
<TopNavigation />
<Body>
{getLayout(
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)}
</Body>
<Footer />
</PageLayout>
</RootStyles>
</ThemeProvider>
</StylesProvider>
);
}

const RootStyles = styled.div`
color: ${({ theme }) => theme.palette.font.primary};
`;
_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
import { ServerStyleSheets } from '@mui/styles';

class MyDocument extends Document {
static async getInitialProps(ctx) {
const styledComponentsSheet = new ServerStyleSheet();
const materialSheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props =>
styledComponentsSheet.collectStyles(
materialSheets.collect(<App {...props} />)
),
});

const initialProps = await Document.getInitialProps(ctx);

return {
...initialProps,
styles: (
<>
{initialProps.styles}
{materialSheets.getStyleElement()}
{styledComponentsSheet.getStyleElement()}
</>
),
};
} finally {
styledComponentsSheet.seal();
}
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
render() {
return (
<Html lang="en" dir="ltr">
<Head>
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}

export default MyDocument;
包.json
...
"dependencies": {
"@emotion/react": "^11.4.1",
"@emotion/styled": "^10.0.27",
"@material-ui/core": "^4.12.1",
"@mui/material": "^5.0.0",
"@mui/styled-engine": "npm:@mui/styled-engine-sc@latest",
"@mui/styles": "^5.0.0",
"next": "10.2.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"storybook-addon-styled-component-theme": "^2.0.0",
"styled-components": "^5.3.1",
},
"resolutions": {
"@mui/styled-engine": "npm:@mui/styled-engine-sc@latest"
},
...

最佳答案

尝试这个

import StyledEngineProvider from '@mui/material/StyledEngineProvider';

function App() {
return (
<StyledEngineProvider injectFirst>
<OtherComponents />
</StyledEngineProvider>
);
}

关于javascript - material ui 'new' v5.0.0 injectionFirst 无法设置特异性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69217739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com