gpt4 book ai didi

css - 为什么 ThemeProvider(Material UI)在这里不适合我?

转载 作者:行者123 更新时间:2023-11-27 23:12:09 25 4
gpt4 key购买 nike

我经常使用 Material UI,所以这令人费解。我查看了文档,检查了我的代码,但看不到问题所在。我希望嵌套组件中的 H2 标签使用 Arial。但是,它是使用 Times 渲染的。我不确定为什么。

这是我的 index.tsx:

import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
import { Provider } from "react-redux";
import configureStore from "./redux/stores/main";
import * as serviceWorker from "./serviceWorker";
import { createMuiTheme } from "@material-ui/core";
import myTheme from "./styling/mainTheme";
import { ThemeProvider } from "@material-ui/styles";

const theme = createMuiTheme({
typography: {
fontFamily: ["Arial"].join(",")
}
});

ReactDOM.render(
<ThemeProvider theme={theme}>
<Provider store={configureStore()}>
<App />
</Provider>
</ThemeProvider>,
document.getElementById("root")
);

serviceWorker.unregister();

我的应用组件:

import React from "react";
import { useSelector } from "react-redux";
import HeaderContainer from "../containers/layout/header/HeaderContainer";
import { ThemeProvider, useTheme } from "@material-ui/styles";
import theme from "../styling/mainTheme";
import { createMuiTheme } from "@material-ui/core";




const App: React.FC = () => {
const theme = useTheme();
return (
<div className="App">
<HeaderContainer />
</div>
);
};

export default App;

header 容器(将包含逻辑):

  import * as React from 'react';
import Header from '../../../components/layout/header/Header';

export interface HeaderContainerProps {
}

export default class HeaderContainer extends React.Component<HeaderContainerProps> {
public render() {
return <Header />
}
}

最后是标题:

import * as React from "react";
import { styled } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";





export default function Header() {
return (
<AppBar>
<h2>Hello</h2>
</AppBar>
)
}

我试过将 ThemeProvider 放在不同的组件中,但我的 h2 仍然呈现为 Times。如果有人能发现问题,那就太好了。谢谢

最佳答案

查看 material-ui 的文档,发现你从库中导入了一些东西,方法不对。就像文档状态一样-

import { useTheme } from '@material-ui/core/styles';
import { createMuiTheme } from '@material-ui/core/styles';

基本可以的

import { useTheme, createMuiTheme } from '@material-ui/core/styles';

ThemeProvider 也是如此

import { ThemeProvider } from '@material-ui/core/styles';

关于css - 为什么 ThemeProvider(Material UI)在这里不适合我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467608/

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