为什么ThemeProvider(材料UI)在这里对我不起作用?

时间:2019-10-19 20:13:49

标签: css reactjs material-ui

我已经使用了很多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;

标头容器(将包含逻辑):

  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。如果有人可以发现问题,那就太好了。谢谢

1 个答案:

答案 0 :(得分:0)

检查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';