简体   繁体   English

MUI React 组件不渲染

[英]MUI React components not rendering

Just started to learn react, and using the mui library.刚开始学react,用的是mui库。

I installed the MUI library with我安装了 MUI 库

npm install @mui/material @emotion/react @emotion/styled

I also installed the roboto font, and the icons.我还安装了 roboto 字体和图标。

Then i made a simple app that should just display MUI button.然后我做了一个简单的应用程序,应该只显示 MUI 按钮。

App.js应用程序.js

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


function App() {
  return (
    <div >
      <h2>Hello World!</h2>
      <Button variant="text">Text</Button>
      <Button variant="contained">Contained</Button>
      <Button variant="outlined">Outlined</Button>
      
    </div>
  );
}

export default App;

Index.js索引.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

The three buttons have been copied directly from the library.这三个按钮已直接从库中复制。 When I run the app with npm start the page remains empty.当我使用npm start运行应用程序时,页面仍然是空的。 The compilation is successful, there are no errors whatsoever.编译成功,没有任何错误。 When i remove the buttons, the <h2> Hello World </h2> suddenly renders.当我删除按钮时, <h2> Hello World </h2>突然呈现。 When the buttons are left in the code, even the <h2> title disappears.当按钮留在代码中时,甚至<h2>标题也会消失。

Why are the components not rendering?为什么组件不渲染?

Found the solution.找到了解决方案。

In my several attempts to get this to work I created multiple apps.在我多次尝试让它工作的过程中,我创建了多个应用程序。 Whenever I created the app using npm create-react-app I would then install the MUI library as described above.每当我使用npm create-react-app ,我都会按上述方式安装 MUI 库。

The issue was that I did not cd to the app directory before installing the library.问题是我在安装库之前没有cd到 app 目录。 After changing directories the Buttons properly rendered.更改目录后,按钮正确呈现。

use this for import用它来导入

import { Button } from '@mui/material';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM