React UI库 MUI 快速入门教程 – wiki基地


🚀 极速上手 MUI:构建现代 React 应用的基石

React 作为构建用户界面的强大库,生态系统日益繁荣。在众多 UI 库中,MUI(前身为 Material UI)凭借其遵循 Material Design 设计理念、丰富的组件、强大的定制能力和活跃的社区,成为了许多 React 开发者构建美观、响应式 Web 应用的首选。

本篇文章旨在提供一个详尽的 MUI 快速入门教程,帮助你从零开始,掌握 MUI 的核心概念和基本使用方法,快速构建起你的第一个使用 MUI 组件的 React 应用。无论你是刚接触 React,还是希望提升现有应用的UI体验,MUI 都能为你提供强大的助力。

我们将从安装开始,逐步讲解如何使用 MUI 的基础组件、如何进行样式定制和主题配置,并通过一个简单的示例,将所学知识融会贯通。

🎯 本教程的目标读者

  • 了解 React 基础知识(组件、JSX、Props、State)。
  • 熟悉 JavaScript (ES6+) 和 npm/yarn/pnpm 包管理器。
  • 希望快速掌握一个优秀的 React UI 库。

✨ 为什么选择 MUI?

在深入学习之前,先来快速了解一下为什么 MUI 如此受欢迎:

  1. 遵循 Material Design: MUI 基于 Google 的 Material Design 设计系统,这是一种现代化、直观且用户体验良好的设计语言,能让你的应用看起来专业且美观。
  2. 丰富的组件库: MUI 提供了一套全面且高质量的 React 组件,涵盖按钮、导航、表单、数据展示、布局等几乎所有常见的 UI 元素,开箱即用。
  3. 强大的定制能力: MUI 提供了灵活的样式解决方案(基于 Emotion 或 styled-components),支持通过 sx prop 进行快速样式覆盖,以及通过主题系统进行全局或局部样式定制,满足各种设计需求。
  4. 响应式设计支持: 许多 MUI 组件内置了响应式特性,配合其布局系统(如 Grid、Stack、Box),可以轻松构建适应不同屏幕尺寸的应用。
  5. 活跃的社区与完善的文档: MUI 拥有庞大的用户群体,遇到问题很容易找到解决方案。官方文档非常详细、清晰,是学习和使用过程中的宝贵资源。
  6. TypeScript 支持: MUI 是用 TypeScript 编写的,提供了完整的类型定义,为使用 TypeScript 的开发者带来了极大的便利。

📚 前置准备

在开始之前,请确保你的开发环境中已安装以下工具:

  • Node.js: 建议安装 LTS 版本。可以通过在终端运行 node -v 检查是否已安装。
  • npm 或 yarn 或 pnpm: Node.js 安装时通常会自带 npm。你也可以选择安装 yarn (npm install -g yarn) 或 pnpm (npm install -g pnpm)。
  • 一个 React 项目: 你可以使用 Create React App、Vite 或 Next.js 等工具快速创建一个 React 项目。本教程的示例将基于一个标准的 React 项目结构。

如果你还没有 React 项目,可以使用 Vite 快速创建一个:

“`bash

使用 npm

npm create vite@latest my-mui-app –template react
cd my-mui-app
npm install
npm run dev

或者使用 yarn

yarn create vite my-mui-app –template react
cd my-mui-app
yarn install
yarn dev

或者使用 pnpm

pnpm create vite my-mui-app –template react
cd my-mui-app
pnpm install
pnpm dev
“`

📦 安装 MUI

安装 MUI 是第一步。MUI 的核心是 @mui/material 包,它包含了所有的 Material Design 组件。此外,你还需要安装一个样式引擎。MUI 默认推荐使用 Emotion,但也支持 styled-components。在本教程中,我们将使用 Emotion。

打开你的 React 项目终端,运行以下命令安装 MUI core 和 Emotion:

“`bash

使用 npm

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

或者使用 yarn

yarn add @mui/material @emotion/react @emotion/styled

或者使用 pnpm

pnpm add @mui/material @emotion/react @emotion/styled
“`

安装完成后,你就可以在项目中使用 MUI 组件了。

图标库安装 (可选但推荐)

MUI 提供了许多基于 Material Design 的图标。如果你需要使用这些图标,还需要额外安装图标库包:

“`bash

使用 npm

npm install @mui/icons-material

或者使用 yarn

yarn add @mui/icons-material

或者使用 pnpm

pnpm add @mui/icons-material
“`

🚀 你的第一个 MUI 组件:按钮 (Button)

现在,让我们在你的 React 应用中添加第一个 MUI 组件——一个简单的按钮。找到你的应用主文件(例如 src/App.jssrc/App.tsx),清空里面的内容,或者在一个新的组件中尝试。

首先,从 @mui/material 中导入 Button 组件:

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

然后在你的组件的 JSX 中使用它,就像使用普通的 HTML 标签一样:

“`jsx
import React from ‘react’;
import Button from ‘@mui/material/Button’;

function App() {
return (

{/ 一个最简单的按钮 /}

);
}

export default App;
“`

启动你的开发服务器 (npm run devyarn devpnpm dev),你会在页面上看到一个蓝色的、带有阴影的按钮。

让我们稍微解释一下这段代码:

  • import Button from '@mui/material/Button';: 从 MUI 库中导入 Button 组件。
  • <Button>...</Button>: 在 JSX 中使用导入的组件。
  • variant="contained": 这是 Button 组件的一个 prop (属性)。MUI 组件通常提供多种 variant 来改变组件的外观。contained 是一个实心背景的按钮,也是 Material Design 中常用的风格。其他常用的 variant 还有 text (文本按钮) 和 outlined (描边按钮)。
  • "Hello MUI Button": 这是按钮的子元素,显示在按钮文本上。

尝试修改 variant 的值,看看按钮样式的变化:

jsx
<Button variant="text">Text Button</Button>
<Button variant="outlined">Outlined Button</Button>
<Button variant="contained">Contained Button</Button>

你还可以通过其他 props 来定制按钮,例如:

  • color: 设置按钮颜色,如 "primary" (默认主题色)、"secondary""success""error""warning", 或者自定义颜色。
  • size: 设置按钮大小,如 "small""medium" (默认)、"large"
  • disabled: 布尔值,设置为 true 禁用按钮。

jsx
<Button variant="contained" color="secondary" size="large">
Secondary Large Button
</Button>
<Button variant="outlined" disabled>
Disabled Button
</Button>

通过简单地使用 variantcolor 等 props,你已经能够快速创建不同风格的按钮了。这是 MUI 组件使用方式的一个典型示例:导入组件,然后通过 props 配置其外观和行为。

📝 使用 Typography 排版文本

除了按钮,文本排版也是 UI 中最基础的部分。MUI 提供了 Typography 组件来标准化文本样式。它基于 Material Design 的排版规范,通过 variant prop 可以轻松应用不同的语义化文本风格,如标题、副标题、正文等。

“`jsx
import React from ‘react’;
import Button from ‘@mui/material/Button’;
import Typography from ‘@mui/material/Typography’; // 导入 Typography

function App() {
return (


Welcome to MUI!


This is a paragraph using body1 variant.


This is another paragraph using body2 variant, which is slightly smaller.

);
}

export default App;
“`

  • import Typography from '@mui/material/Typography';: 导入 Typography 组件。
  • variant: 设置文本的样式风格,如 "h1""h2""h6" (对应 HTML 的 <h1><h6> 标签,但实际渲染的 HTML 标签可以通过 component prop 控制),"body1" (默认正文)、"body2""subtitle1""subtitle2""caption""overline" 等。
  • gutterBottom: 布尔值,设置为 true 会在文本下方添加一些间距,常用于段落或标题下方。

Typography 组件不仅提供了样式,还能帮助你更好地管理文本的语义结构。默认情况下,variant="h1" 会渲染成 <h1> 标签,variant="body1" 会渲染成 <p> 标签。如果你需要使用不同的 HTML 标签,可以使用 component prop:

jsx
{/* 样式是 h1,但实际渲染成 div 标签 */}
<Typography variant="h1" component="div">
Styled like H1, rendered as DIV
</Typography>

🖼️ 添加图标 (Icons)

现代应用中,图标是不可或缺的一部分。MUI 提供了 @mui/icons-material 包,其中包含了 Material Design 官方的图标库。

首先确保你已经安装了 @mui/icons-material

然后,你可以像导入普通 React 组件一样导入并使用任何图标。图标的名称通常是其描述的驼峰命名加上 Icon 后缀,例如 HomeIcon, MenuIcon, DeleteIcon

“`jsx
import React from ‘react’;
import Button from ‘@mui/material/Button’;
import Typography from ‘@mui/material/Typography’;
import HomeIcon from ‘@mui/icons-material/Home’; // 导入一个图标
import DeleteIcon from ‘@mui/icons-material/Delete’; // 导入另一个图标

function App() {
return (


Examples with Icons

  {/* 在文本中直接使用图标 */}
  <HomeIcon />
  <Typography variant="body1" component="span" sx={{ verticalAlign: 'middle', ml: 0.5 }}>
    Home Page
  </Typography>

  {/* 在按钮中使用图标 */}
  <Button variant="contained" startIcon={<DeleteIcon />}>
    Delete
  </Button>
  <Button variant="outlined" endIcon={<HomeIcon />}>
    Home
  </Button>
</div>

);
}

export default App;
“`

  • import HomeIcon from '@mui/icons-material/Home';: 导入 Home 图标。
  • <HomeIcon />: 直接在 JSX 中渲染图标组件。
  • startIcon / endIcon: Button 等组件提供了这些 props,可以方便地在文本前或后添加图标。你需要将图标组件作为 JSX 元素传递给这些 props。
  • sx={{ verticalAlign: 'middle', ml: 0.5 }}: 这是一个快速应用自定义样式的例子,我们稍后会详细讲解 sx prop。这里用于让图标和文本垂直对齐,并添加左边距。

🧱 使用 Box 和 Stack 进行布局

MUI 提供了一些实用的布局组件,其中 BoxStack 是最常用、最基础的两个。它们是构建复杂布局的基石。

  • Box: 可以看作是一个增强版的 <div><span>。它提供了直接访问主题中定义的值(如间距、颜色)以及一些常用的 CSS 属性的快捷方式,最常用于应用间隔 (padding/margin)、颜色、边框、flexbox/grid 布局等样式。
  • Stack: 用于管理其子元素的一维布局(水平或垂直方向),通过 spacing prop 可以轻松控制子元素之间的间隔,无需手动添加 margin。

“`jsx
import React from ‘react’;
import Box from ‘@mui/material/Box’; // 导入 Box
import Stack from ‘@mui/material/Stack’; // 导入 Stack
import Button from ‘@mui/material/Button’;
import Typography from ‘@mui/material/Typography’;

function App() {
return (


Layout Examples

  {/* 使用 Stack 垂直布局按钮,间隔为 2 (对应主题中的间距单位) */}
  <Stack spacing={2} direction="column" sx={{ mb: 2 }}>
    <Button variant="contained">Button 1</Button>
    <Button variant="outlined">Button 2</Button>
    <Button variant="text">Button 3</Button>
  </Stack>

  {/* 使用 Stack 水平布局,间隔为 3 */}
  <Stack spacing={3} direction="row">
    <Box sx={{ width: 50, height: 50, backgroundColor: 'primary.main' }} />
    <Box sx={{ width: 50, height: 50, backgroundColor: 'secondary.main' }} />
    <Box sx={{ width: 50, height: 50, backgroundColor: 'error.main' }} />
  </Stack>
</Box>

);
}

export default App;
“`

这段代码引入了 BoxStack,并展示了它们的基本用法:

  • 最外层使用 Box 作为容器,应用了 maxWidth、水平居中 (mx: 'auto')、顶部外边距 (mt: 4)、内边距 (p: 2) 和边框等样式。这里的 sx prop 是关键,它允许我们直接在组件上应用样式,并且支持访问主题值(如 mt: 4 对应 theme.spacing(4))。
  • 第一个 Stack 示例展示了如何通过 spacing={2} 在垂直方向 (direction="column") 为其子元素(三个按钮)添加间隔。mb: 2 在 Stack 底部添加外边距。
  • 第二个 Stack 示例展示了如何通过 spacing={3} 在水平方向 (direction="row") 为三个 Box 添加间隔。这里的 Box 被用作一个简单的色块,通过 sx 设置了宽度、高度和背景色 (primary.main 等是访问主题颜色值的语法,我们后面会讲)。

BoxStack 结合 sx prop,提供了非常灵活且高效的布局和样式应用方式。Stack 非常适合简单的线性布局,而 Box 则是一个全能的样式和布局容器。

🎨 样式定制:sx Prop 的力量

在前面的例子中,我们已经多次看到了 sx prop。它是 MUI 中最常用的样式定制方法之一,允许你在组件上直接应用 CSS 样式,并且能够方便地访问主题(Theme)中定义的颜色、间距、字体等变量。

sx prop 接受一个对象或一个数组对象,对象中的键值对通常对应 CSS 属性及其值。MUI 对一些常用的 CSS 属性提供了简写,并与主题中的间距单位关联起来。

例如:

  • margin: m
  • padding: p
  • marginTop: mt
  • marginBottom: mb
  • marginLeft: ml
  • marginRight: mr
  • paddingTop: pt
  • paddingBottom: pb
  • paddingLeft: pl
  • paddingRight: pr
  • marginX: mx (设置左右外边距)
  • paddingX: px (设置左右内边距)
  • marginY: my (设置上下外边距)
  • paddingY: py (设置上下内边距)

这些简写属性的值可以是数字(与主题的 spacing 单位相乘,默认 1 单位是 8px),也可以是标准的 CSS 值(如 '16px', 'auto', '50%')。

示例:

“`jsx
<Box
sx={{
mt: 3, // margin-top: theme.spacing(3) = 24px
mb: 1, // margin-bottom: theme.spacing(1) = 8px
px: 2, // padding-left & padding-right: theme.spacing(2) = 16px
backgroundColor: ‘primary.light’, // 访问主题中的 primary 颜色
color: ‘primary.contrastText’, // 访问主题中的对比色
borderRadius: 1, // border-radius: theme.shape.borderRadius * 1
display: ‘flex’, // 标准 CSS 属性
justifyContent: ‘center’,
alignItems: ‘center’,
height: 50,
}}

这是一个带有 sx 样式的 Box

“`

响应式样式: sx prop 还支持数组语法来实现响应式样式,非常强大。数组中的值对应不同的断点(breakpoints),默认断点为 ['xs', 'sm', 'md', 'lg', 'xl']

“`jsx
<Typography
variant=”h3″
sx={{
fontSize: [ ‘1.5rem’, ‘2rem’, ‘3rem’ ], // xs: 1.5rem, sm: 2rem, md+: 3rem
textAlign: [‘center’, ‘left’], // xs: center, sm+: left
color: {
xs: ‘primary.main’, // 小屏幕使用主色
sm: ‘secondary.main’, // 中等屏幕使用副色
md: ‘error.main’ // 大屏幕使用错误色
}
}}

Responsive Text

“`

在这个例子中:
* fontSize 会在超小屏幕 (xs) 下是 1.5rem,小屏幕 (sm) 下是 2rem,中等及以上屏幕 (md, lg, xl) 下是 3rem
* textAlign 会在超小屏幕 (xs) 下是 center,小屏幕及以上屏幕 (sm, md, lg, xl) 下是 left
* color 演示了如何使用对象语法为每个断点指定不同的颜色,直接通过断点名称作为键。

sx prop 极大地简化了组件级别的样式覆盖和响应式处理,是使用 MUI 时必须掌握的核心技巧。

🖼️ 主题配置 (Theming)

虽然 sx prop 允许你覆盖单个组件的样式,但如果你想改变应用整体的风格,例如主色调、字体、圆角等,就需要使用 MUI 的主题系统。

主题是一个包含了设计规范的对象,MUI 的所有组件都会使用这个主题对象中定义的值。通过创建一个自定义主题并将其提供给应用,你可以轻松地统一整个应用的视觉风格。

  1. 创建主题: 使用 createTheme 函数创建一个主题对象。你可以覆盖默认主题的很多属性,最常见的是调色板 (palette)、排版 (typography)、间距 (spacing) 等。

    “`javascript
    // src/theme.js (或者其他你喜欢的文件名)
    import { createTheme } from ‘@mui/material/styles’;
    import { red, purple } from ‘@mui/material/colors’; // 导入一些预设颜色

    const theme = createTheme({
    palette: {
    primary: {
    main: purple[500], // 设置主色为紫色
    },
    secondary: {
    main: red[500], // 设置副色为红色
    },
    // 你还可以添加更多颜色,如 error, warning, info, success
    },
    typography: {
    fontFamily: [ // 可以指定字体栈
    ‘-apple-system’,
    ‘BlinkMacSystemFont’,
    ‘”Segoe UI”‘,
    ‘Roboto’,
    ‘”Helvetica Neue”‘,
    ‘Arial’,
    ‘sans-serif’,
    ‘”Apple Color Emoji”‘,
    ‘”Segoe UI Emoji”‘,
    ‘”Segoe UI Symbol”‘,
    ].join(‘,’),
    // 你还可以定制其他排版样式,如 fontSize, h1, body1 等
    },
    spacing: 8, // 定义间距单位,默认也是 8px
    shape: {
    borderRadius: 4, // 定义圆角,默认也是 4px
    },
    // 还可以定制组件的默认 props 或样式
    // components: {
    // MuiButton: {
    // defaultProps: {
    // variant: ‘outlined’, // 将所有按钮的默认变体改为 outlined
    // },
    // styleOverrides: { // 覆盖按钮的样式
    // root: {
    // textTransform: ‘none’, // 按钮文本不做大写处理
    // },
    // },
    // },
    // },
    });

    export default theme;
    “`

  2. 提供主题: 使用 ThemeProvider 组件将你创建的主题提供给你的应用根组件或需要应用主题的部分。ThemeProvider 来自 @mui/material/styles

    “`jsx
    // src/App.js (或者你的根组件文件)
    import React from ‘react’;
    import { ThemeProvider } from ‘@mui/material/styles’; // 导入 ThemeProvider
    import theme from ‘./theme’; // 导入你创建的主题文件
    import Button from ‘@mui/material/Button’;
    import Typography from ‘@mui/material/Typography’;
    import Box from ‘@mui/material/Box’;
    import Stack from ‘@mui/material/Stack’;
    import HomeIcon from ‘@mui/icons-material/Home’;

    function App() {
    return (
    // 将整个应用包裹在 ThemeProvider 中



    My Themed App

        {/* 按钮会自动使用主题中的颜色 */}
        <Button variant="contained" color="primary" sx={{ mr: 2 }}>
          Primary Button
        </Button>
        <Button variant="contained" color="secondary">
          Secondary Button
        </Button>
    
        {/* Stack 和 Box 也可以访问主题颜色和间距 */}
        <Stack direction="row" spacing={ theme.spacing(1) } sx={{ mt: 3 }}> {/* 直接使用 theme.spacing(1) 或简写 1 */}
            <Box sx={{
                width: 50, height: 50,
                bgcolor: 'primary.dark', // 使用 bgcolor 简写 background-color
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                borderRadius: '50%' // 圆形
            }}>
                <HomeIcon sx={{ color: 'primary.contrastText' }} />
            </Box>
             <Box sx={{
                width: 50, height: 50,
                bgcolor: 'secondary.dark',
                 display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                borderRadius: '50%'
            }}>
                 <HomeIcon sx={{ color: 'secondary.contrastText' }} />
            </Box>
        </Stack>
      </Box>
    </ThemeProvider>
    

    );
    }

    export default App;
    “`

现在,你的应用中的所有 MUI 组件都会使用你定义的主题颜色(紫色和红色)。

主题系统是 MUI 定制的核心,它不仅能让你统一颜色和排版,还可以定制组件的默认属性、样式覆盖、添加自定义变量等,是构建品牌一致性强的应用的关键。

简单的组件组合示例

让我们用前面学到的知识,构建一个简单的卡片组件。

“`jsx
import React from ‘react’;
import { ThemeProvider } from ‘@mui/material/styles’;
import theme from ‘./theme’; // 假设你创建了主题文件
import Box from ‘@mui/material/Box’;
import Card from ‘@mui/material/Card’; // 导入 Card 组件
import CardContent from ‘@mui/material/CardContent’; // 导入 CardContent 组件
import CardActions from ‘@mui/material/CardActions’; // 导入 CardActions 组件
import Button from ‘@mui/material/Button’;
import Typography from ‘@mui/material/Typography’;
import HomeIcon from ‘@mui/icons-material/Home’;

function SimpleCard() {
return (
{/ 设置卡片最大宽度 /}
{/ 卡片内容区域 /}

Word of the Day


be-nev-o-lent


adjective


well meaning and kindly.

{‘”a benevolent smile”‘}


{/ 卡片底部动作区域 /}



);
}

function App() {
return (





)
}

export default App;

“`

在这个例子中,我们使用了 Card, CardContent, CardActions 组件来构建一个卡片。Card 是容器,CardContent 用于包裹主要内容,CardActions 用于放置按钮等操作元素。我们再次使用了 Typography 来排版文本,并使用 sx prop 在 Typography 上应用了自定义字体大小、颜色和底部外边距。外层 Box 用于将卡片在屏幕中居中并添加背景色。

这个例子展示了如何将不同的 MUI 组件组合起来,以及如何使用 sx prop 进行微调。

进阶之路:下一步去哪里?

恭喜你!通过本教程,你已经掌握了 MUI 的基本安装、常用组件的使用、sx prop 进行样式定制以及主题配置。这为你深入学习 MUI 打下了坚实的基础。

MUI 还有更多强大的特性和组件等待你去探索,例如:

  • 更丰富的组件: AppBar (应用栏), Drawer (抽屉导航), Dialog (对话框), Menu (菜单), Table (表格), Form Controls (各种表单输入框), DataGrid (高级表格) 等等。
  • 布局系统 Grid: 提供了强大的 12 列网格布局系统,是构建复杂响应式布局的利器。
  • Hooks: useMediaQuery (用于响应式逻辑), useTheme (在组件中访问主题对象) 等。
  • 自定义组件样式: 除了 sx prop 和主题,你还可以使用 styled API 或其他 CSS-in-JS 方法来构建完全自定义的组件样式。
  • 不同的样式方案: 了解 Emotion 和 styled-components 的区别和使用。
  • SSR (Server-Side Rendering) 支持: 如果你使用 Next.js 等框架,需要配置 SSR 才能让 MUI 样式正确渲染。

📖 学习资源

  • MUI 官方文档: 这是学习 MUI 最权威、最全面的资源。务必多查阅组件的 props、CSS API 和演示示例。
  • MUI GitHub 仓库: 查看源代码、提 Issue、参与贡献。
  • Stack Overflow: 搜索你遇到的问题,很可能已经被解答了。
  • MUI 社区: 加入 Discord 或其他社区,与其他开发者交流。

总结

MUI 是一个功能强大、灵活且美观的 React UI 库。遵循 Material Design 设计理念,提供了丰富的开箱即用组件。通过掌握其安装、基础组件使用、sx prop 样式定制和主题配置,你就能快速构建出具有专业外观和优秀用户体验的 React 应用。

记住,最好的学习方法是动手实践。尝试在你自己的项目中集成 MUI,多查阅官方文档,你会越来越熟练地使用这个强大的工具。

希望这篇详细的快速入门教程对你有所帮助!祝你在使用 MUI 的旅程中一帆风顺!


发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部