“Material-ui AppBar 组件未更改背景颜色”

huangapple go评论97阅读模式
英文:

Material-ui AppBar component not changing background-color

问题

以下是您提供的内容的翻译:

我在我的React应用程序中遇到了这个错误已经有一段时间了,但似乎找不到解决方法或逻辑解释。我正在使用Material-UI v6和styled-components。
我有这个样式化组件(AppBar.styled.js):

  1. import styled from "styled-components";
  2. import { AppBar } from "@mui/material";
  3. const StyledAppBar = styled(AppBar)`
  4. margin: 20px; //${(props) => props.theme.palette.tertiary.main};
  5. background-color: darkorange;
  6. `;
  7. export default StyledAppBar;

奇怪的是,margin属性正确应用于应用栏,但background-color没有:
“Material-ui AppBar 组件未更改背景颜色”

我似乎找不出原因,也找不到解决方法。有人可以解释发生了什么以及我如何解决吗?我已经尝试添加StyledEngineProvider,但仍然没有变化。这是App.js的样子:

  1. import { Fragment } from "react";
  2. import GlobalStyles from "./components/styles/Global";
  3. import darkTheme from "./components/styles/darkTheme";
  4. import lightTheme from "./components/styles/lightTheme";
  5. import { ThemeProvider } from "styled-components";
  6. import { createBrowserRouter, RouterProvider } from "react-router-dom";
  7. import HomePage from "./pages/Home";
  8. import HelpPage from "./pages/Help";
  9. import NavigationBar from "./components/NavigationBar";
  10. const router = createBrowserRouter([
  11. { path: "/", element: <HomePage /> },
  12. { path: "/help", element: <HelpPage /> },
  13. ]);
  14. function App() {
  15. const theme = lightTheme;
  16. return (
  17. <ThemeProvider theme={theme}>
  18. <Fragment>
  19. <GlobalStyles></GlobalStyles>
  20. <NavigationBar></NavigationBar>
  21. <RouterProvider router={router}></RouterProvider>
  22. </Fragment>
  23. </ThemeProvider>
  24. );
  25. }
  26. export default App;
英文:

I have been in this error for quite a while now in my React application but can't seem to find a solution nor a logical explanation. I'm using Material-UI v6 and styled-components.
I have this styled component (AppBar.styled.js):

  1. import styled from &quot;styled-components&quot;;
  2. import { AppBar } from &quot;@mui/material&quot;;
  3. const StyledAppBar = styled(AppBar)`
  4. margin: 20px; //${(props) =&gt; props.theme.palette.tertiary.main};
  5. background-color: darkorange;
  6. `;
  7. export default StyledAppBar;

And curiously, the margin property is correctly applied to the appbar but the background-color is not:
“Material-ui AppBar 组件未更改背景颜色”

I can't seem to figure out why nor a solution. Can some one care to exaplain what is happening and how can I solve this? I already tried to add the StyledEngineProvider but still no changes. This is how the App.js looks like:

  1. import { Fragment } from &quot;react&quot;;
  2. import GlobalStyles from &quot;./components/styles/Global&quot;;
  3. import darkTheme from &quot;./components/styles/darkTheme&quot;;
  4. import lightTheme from &quot;./components/styles/lightTheme&quot;;
  5. import { ThemeProvider } from &quot;styled-components&quot;;
  6. import { createBrowserRouter, RouterProvider } from &quot;react-router-dom&quot;;
  7. import HomePage from &quot;./pages/Home&quot;;
  8. import HelpPage from &quot;./pages/Help&quot;;
  9. import NavigationBar from &quot;./components/NavigationBar&quot;;
  10. const router = createBrowserRouter([
  11. { path: &quot;/&quot;, element: &lt;HomePage /&gt; },
  12. { path: &quot;/help&quot;, element: &lt;HelpPage /&gt; },
  13. ]);
  14. function App() {
  15. const theme = lightTheme;
  16. return (
  17. &lt;ThemeProvider theme={theme}&gt;
  18. &lt;Fragment&gt;
  19. &lt;GlobalStyles&gt;&lt;/GlobalStyles&gt;
  20. &lt;NavigationBar&gt;&lt;/NavigationBar&gt;
  21. &lt;RouterProvider router={router}&gt;&lt;/RouterProvider&gt;
  22. &lt;/Fragment&gt;
  23. &lt;/ThemeProvider&gt;
  24. );
  25. }
  26. export default App;

答案1

得分: 0

我找到了一个解决方案,即使用"!important"关键字,但这不是推荐的方法。但还有另一个诀窍:在App.js中的返回JSX代码中使用<StyledEngineProvider injectFirst>...</StyledEngineProvider>,这是正确且有效的解决方法。
它也被官方文档推荐使用。

英文:

I found a solution, which was to use the "!important" keyword but this is not recommended. But there is another trick: the correct and working solution is to use &lt;StyledEngineProvider injectFirst&gt;...&lt;/StyledEngineProvider&gt; in the returning JSX code in the App.js.
It is also recommended by the official documentation.

答案2

得分: -1

另一种实现这一目标的方式是通过更改主题。您可以在此示例中查看。

https://www.brainstormcreative.co.uk/material-ui/how-to-change-the-material-ui-appbar-background-color/

英文:

Another way to achive this is by changing it from theme. You can see in this example.

https://www.brainstormcreative.co.uk/material-ui/how-to-change-the-material-ui-appbar-background-color/

huangapple
  • 本文由 发表于 2023年4月11日 03:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定