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

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

Material-ui AppBar component not changing background-color

问题

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

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

import styled from "styled-components";
import { AppBar } from "@mui/material";

const StyledAppBar = styled(AppBar)`
  margin: 20px; //${(props) => props.theme.palette.tertiary.main};
  background-color: darkorange;
`;

export default StyledAppBar;

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

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

import { Fragment } from "react";
import GlobalStyles from "./components/styles/Global";
import darkTheme from "./components/styles/darkTheme";
import lightTheme from "./components/styles/lightTheme";
import { ThemeProvider } from "styled-components";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import HomePage from "./pages/Home";
import HelpPage from "./pages/Help";
import NavigationBar from "./components/NavigationBar";

const router = createBrowserRouter([
   { path: "/", element: <HomePage /> },
   { path: "/help", element: <HelpPage /> },
]);

function App() {
  const theme = lightTheme;
  return (
    <ThemeProvider theme={theme}>
      <Fragment>
        <GlobalStyles></GlobalStyles>
        <NavigationBar></NavigationBar>
        <RouterProvider router={router}></RouterProvider>
      </Fragment>
    </ThemeProvider>
  );
}

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):

import styled from &quot;styled-components&quot;;
import { AppBar } from &quot;@mui/material&quot;;

const StyledAppBar = styled(AppBar)`
  margin: 20px; //${(props) =&gt; props.theme.palette.tertiary.main};
  background-color: darkorange;
`;

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:

import { Fragment } from &quot;react&quot;;
import GlobalStyles from &quot;./components/styles/Global&quot;;
import darkTheme from &quot;./components/styles/darkTheme&quot;;
import lightTheme from &quot;./components/styles/lightTheme&quot;;
import { ThemeProvider } from &quot;styled-components&quot;;
import { createBrowserRouter, RouterProvider } from &quot;react-router-dom&quot;;
import HomePage from &quot;./pages/Home&quot;;
import HelpPage from &quot;./pages/Help&quot;;
import NavigationBar from &quot;./components/NavigationBar&quot;;

  const router = createBrowserRouter([
     { path: &quot;/&quot;, element: &lt;HomePage /&gt; },
     { path: &quot;/help&quot;, element: &lt;HelpPage /&gt; },
    ]);

  function App() {
    const theme = lightTheme;
    return (
     &lt;ThemeProvider theme={theme}&gt;
     &lt;Fragment&gt;
       &lt;GlobalStyles&gt;&lt;/GlobalStyles&gt;
        &lt;NavigationBar&gt;&lt;/NavigationBar&gt;
        &lt;RouterProvider router={router}&gt;&lt;/RouterProvider&gt;
     &lt;/Fragment&gt;
    &lt;/ThemeProvider&gt;
  );
 }

 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:

确定