Skip to content
Tutorials Center
My learning

Topics

Lesson 97 of 132 ยท Ecosystem

Material UI and React Bootstrap in Practice

Use Material UI and React Bootstrap in practice - install, core components, ThemeProvider, sx and styled, CssBaseline, grid and layout - with runnable stand-ins that show how theming and variants work.

The UI libraries lesson compared the options and built a tiny design system. This lesson is the hands-on companion for the two libraries that beginners meet first: Material UI (MUI), which implements Google's Material Design, and React Bootstrap, which wraps the Bootstrap CSS framework in React components. For each one you will see installation, the core components, how theming works and the mistakes that cost time.

Neither package can be installed in the editor, so the real code is read-only. After each library there is a runnable stand-in: a few dozen lines that reproduce the mechanism (a theme object in context, an sx prop, variant classes) so you understand what the library does for you. The stand-ins are not the real libraries.

Material UI

Install and first component

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

MUI renders its styles with Emotion (a CSS-in-JS engine), which is why those two extra packages are installed. Icons come from @mui/icons-material. A first component:

JSX
// read-only
import Button from '@mui/material/Button';

export default function App() {
  return <Button variant="contained">Save</Button>;
}

Importing from the component's own path (@mui/material/Button) keeps your development bundle small; import { Button } from '@mui/material' also works and is tree-shaken in production builds.

The components you will use every day

Area Components
Layout Box, Container, Stack, Grid, Paper
Inputs Button, TextField, Select, Checkbox, Switch, Slider
Navigation AppBar and Toolbar, Tabs, Drawer, Breadcrumbs, Menu
Surfaces Card, Accordion, Dialog
Feedback Alert, Snackbar, CircularProgress, Skeleton
Data display Typography, Table, List, Chip, Avatar
JSX
// read-only: a small login card
import { Card, CardContent, CardActions, TextField, Button, Typography, Stack } from '@mui/material';

function LoginCard() {
  return (
    <Card sx={{ maxWidth: 360, mx: 'auto', mt: 4 }}>
      <CardContent>
        <Typography variant="h5" component="h1" gutterBottom>Sign in</Typography>
        <Stack spacing={2}>
          <TextField label="Email" type="email" fullWidth />
          <TextField label="Password" type="password" fullWidth />
        </Stack>
      </CardContent>
      <CardActions sx={{ justifyContent: 'flex-end', p: 2 }}>
        <Button variant="contained">Sign in</Button>
      </CardActions>
    </Card>
  );
}

Note component="h1": Typography variant="h5" decides how the text looks, component decides which HTML element it is. Keep headings in a sensible order for accessibility.

Theming: ThemeProvider and createTheme

Every MUI component reads its colors, fonts, spacing and shapes from a theme supplied through context (see Context). You create a theme with createTheme and provide it once near the root:

JSX
// read-only
import { createTheme, ThemeProvider, CssBaseline } from '@mui/material';

const theme = createTheme({
  palette: {
    mode: 'light',                 // 'dark' gives a dark theme
    primary: { main: '#2563eb' },
    secondary: { main: '#db2777' },
  },
  typography: { fontFamily: 'system-ui, sans-serif' },
  shape: { borderRadius: 10 },
  components: {
    MuiButton: { defaultProps: { disableElevation: true } },
  },
});

root.render(
  <ThemeProvider theme={theme}>
    <CssBaseline />
    <App />
  </ThemeProvider>
);

CssBaseline resets browser defaults and applies the theme's background and text color to the page, so you should include it once, inside the provider. The components key changes defaults or style overrides for every instance of a component, which is how you apply a design system without touching each use.

The sx prop and styled

sx is a prop on every MUI component that accepts a style object with access to the theme. Values such as p: 2 mean "2 times the spacing unit" (8px by default), color: 'primary.main' reads from the palette, and responsive values use objects or arrays: { xs: 1, md: 3 }.

JSX
// read-only
<Box sx={{ p: 2, bgcolor: 'background.paper', color: 'text.secondary', borderRadius: 2, width: { xs: '100%', md: 400 } }}>
  Responsive box
</Box>

For a reusable styled version of a component, use styled:

JSX
// read-only
import { styled } from '@mui/material/styles';
import Button from '@mui/material/Button';

const PillButton = styled(Button)(({ theme }) => ({
  borderRadius: 999,
  paddingInline: theme.spacing(3),
}));

Dark mode and the Grid

Setting palette.mode flips colors for every component. In recent versions you can also use colorSchemes for system-preference support. The Grid component lays out in 12 columns; the current version takes a size prop (<Grid size={{ xs: 12, md: 6 }}>), while older tutorials use item xs={12} md={6}, so check the version in your project.

A stand-in: theme in context, sx and variants

The demo below builds the same three ideas from scratch: a theme object in context, a Button with variant and color props that read it, and an sx-like prop where numbers mean spacing units and strings like primary.main look up the palette.

Stand-in: ThemeProvider, variants and an sx prop

Swapping the theme object restyles every component under the provider, with no props changed on the buttons. That is the whole value of a theme.

React Bootstrap

Install and the CSS

Terminal
npm install react-bootstrap bootstrap

React Bootstrap provides components, not styles. The styles come from the Bootstrap CSS, which you import once (and you can replace with a customized build):

JSX
// main.jsx (read-only)
import 'bootstrap/dist/css/bootstrap.min.css';

Forgetting this import is the number one beginner mistake: the components render, but look unstyled.

Layout and components

React Bootstrap mirrors the Bootstrap markup. The 12-column grid is Container, Row and Col, with the same breakpoints (xs, sm, md, lg, xl, xxl):

JSX
// read-only
import { Container, Row, Col, Card, Button, Navbar, Nav, Form, Alert, Modal } from 'react-bootstrap';

function Products({ items }) {
  return (
    <Container>
      <Navbar bg="dark" data-bs-theme="dark" expand="md" className="mb-3">
        <Container>
          <Navbar.Brand href="#">Shop</Navbar.Brand>
          <Nav><Nav.Link href="#cart">Cart</Nav.Link></Nav>
        </Container>
      </Navbar>
      <Row xs={1} md={3} className="g-3">
        {items.map((item) => (
          <Col key={item.id}>
            <Card>
              <Card.Body>
                <Card.Title>{item.name}</Card.Title>
                <Card.Text>${item.price}</Card.Text>
                <Button variant="primary">Add to cart</Button>
              </Card.Body>
            </Card>
          </Col>
        ))}
      </Row>
    </Container>
  );
}

Components use compound names (Card.Body, Nav.Link, Form.Control), the pattern you built in Compound Components. Variants such as primary, danger and outline-secondary become Bootstrap classes (btn btn-primary).

A controlled dialog shows how state and the library meet:

JSX
// read-only
function ConfirmDelete({ onConfirm }) {
  const [show, setShow] = useState(false);
  return (
    <>
      <Button variant="outline-danger" onClick={() => setShow(true)}>Delete</Button>
      <Modal show={show} onHide={() => setShow(false)} centered>
        <Modal.Header closeButton><Modal.Title>Delete item?</Modal.Title></Modal.Header>
        <Modal.Body>This cannot be undone.</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={() => setShow(false)}>Cancel</Button>
          <Button variant="danger" onClick={() => { onConfirm(); setShow(false); }}>Delete</Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}

show and onHide make the modal a controlled component. The library handles focus trapping, Escape and the backdrop. Dark mode uses the data-bs-theme attribute on any element; and since the styling lives in CSS, customizing means overriding Sass variables or CSS custom properties, rather than a theme object.

A stand-in: how variant props become classes

Stand-in: variant props become Bootstrap-style classes

Choosing between them

Material UI React Bootstrap
Look Material Design, distinctive Classic Bootstrap, easy to recognize
Styling system Emotion, sx, theme object Bootstrap CSS and Sass variables
Theming Deep (createTheme, per-component overrides) Variables and data-bs-theme
Bundle Larger runtime (CSS-in-JS) Smaller JS, CSS shipped separately
Components Very large set, plus data grid (some paid) Core Bootstrap set
Best for Dashboards and apps wanting a full design system Quick sites, teams that already know Bootstrap

If you want full control of the look, a headless library such as Radix with Tailwind (see UI Libraries) fits better. For the underlying CSS skills, the Bootstrap tutorial explains the framework itself.

Common mistakes

  • Missing the Bootstrap CSS import (React Bootstrap) or CssBaseline and ThemeProvider (MUI).
  • Importing a different Bootstrap JavaScript bundle next to React Bootstrap. The components already handle behavior; do not add bootstrap.bundle.js and jQuery-style data attributes for modals and dropdowns.
  • Mixing two styling systems on the same element without a plan, so specificity decides who wins. Use sx, styled or theme overrides for MUI, and CSS classes for Bootstrap.
  • Following a tutorial from another major version. MUI's Grid and React Bootstrap's Form APIs changed between versions; check the version in package.json.
  • Customizing by overriding generated class names (.css-1x2y3z). Use theme overrides or documented slots instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does ThemeProvider do in Material UI?

    The theme is context. Swapping it restyles all MUI components beneath the provider.
  2. 2. What is the sx prop?

    sx accepts CSS with shorthand and theme lookups, for example p: 2 for spacing or color 'primary.main' for the palette.
  3. 3. Your React Bootstrap buttons render but look plain. What is the most likely cause?

    React Bootstrap provides components only. The look comes from bootstrap's CSS, which you import once at the app entry.
  4. 4. Why does Typography in MUI have both variant and component props?

    Looks and semantics are separate. You can style text like h5 while rendering a real h1 for structure and accessibility.
  5. 5. Which choice suits a team that wants complete control over the look and only needs behavior from the library?

    Headless libraries supply accessible behavior and leave all styling to you, unlike MUI and React Bootstrap which ship a look.

Summary

  • MUI needs @mui/material plus Emotion; theme it with createTheme, ThemeProvider and CssBaseline, and style with sx and styled.
  • React Bootstrap needs react-bootstrap and the Bootstrap CSS; layout uses Container, Row, Col, and components are compound (Card.Body).
  • Both manage focus, keyboard behavior and ARIA for dialogs, menus and tabs, which is the main reason to use a library.
  • Theme in context plus variant props that map to styles is the mechanism under both.
  • Check the major version of a library before copying examples.

Next: The React Ecosystem Map