Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Components and Props

Common Props: component, ref and slots

Learn the props almost every Material UI component shares: component for polymorphism, ref, className, sx, slots and slotProps, and how extra props are passed down.

After a few components you notice that MUI is consistent. Button, Chip, Paper and Typography all take sx, className, ref, component, and often variant, color, size and disabled. Learning these shared props once lets you use any new component with confidence. This lesson covers the shared props and the most powerful one: component.

The prop vocabulary

Prop Meaning
children Content inside the component.
sx Style object with theme access (see the sx lesson).
className Add your own CSS class.
style Inline style. Works, but sx is usually better.
ref Reference to the root DOM element.
component Render a different element or component.
variant Which visual flavor (contained, outlined, standard...).
color A palette color (primary, error...).
size small, medium, large (where supported).
disabled Disable interaction.
slots, slotProps Replace or configure inner parts.
other props Passed to the root element: id, aria-*, data-*, event handlers.

That last row matters: any prop MUI does not use itself is forwarded to the root DOM element. So <Button data-testid="save" aria-describedby="hint" onMouseEnter={...}> just works, with no special API.

The component prop

A MUI component picks a default HTML element, but with the component prop you can change what it renders while keeping its styling and behavior. This is called a polymorphic component.

JSX
import Typography from '@mui/material/Typography';
import Paper from '@mui/material/Paper';
import Button from '@mui/material/Button';
import { Link as RouterLink } from 'react-router-dom';

<Typography component="h1" variant="h3">An h1 styled as h3</Typography>
<Paper component="section" sx={{ p: 2 }}>A section element</Paper>
<Paper component="form" onSubmit={handleSubmit}>A form surface</Paper>
<Button component={RouterLink} to="/settings">Settings</Button>
<Button component="a" href="/docs" variant="outlined">Docs</Button>

With component={RouterLink}, extra props like to are forwarded to the router link. This is how you integrate MUI with React Router, Next.js Link or any custom link component without losing the button look.

With TypeScript, MUI's polymorphic typing knows the props of the element you chose, so to is accepted only when component={RouterLink}.

If you use the same router link on many MUI components, set it once in the theme with components.MuiButtonBase.defaultProps.LinkComponent. Then plain href props use your router everywhere. The theming chapter shows how to set defaults.

Stand-in: building a polymorphic component

The best way to understand component is to write one. This stand-in Text component accepts component, forwards every other prop to the root, merges className, and exposes the DOM node through ref. React 19 passes ref as an ordinary prop for function components, so no forwardRef is needed (real MUI handles older React versions for you).

Stand-in: a polymorphic Text component

ref and refs to inner elements

ref gives you the root DOM element, for example to focus an input or measure a box:

JSX
const buttonRef = useRef(null);
<Button ref={buttonRef}>Save</Button>;
// buttonRef.current is the <button> element

Inner elements have dedicated props. For TextField, inputRef points to the actual <input> (use it to call .focus()), while ref points to the outer wrapper. When you build your own component that wraps a MUI one and want to expose the DOM node, pass ref through (React 19 treats it as a normal prop; with React 18 use forwardRef).

className and the inner-class convention

Every MUI component adds stable class names such as MuiButton-root, MuiButton-containedPrimary and state classes like Mui-disabled, Mui-focused, Mui-selected. You can use them in CSS or in sx selectors:

JSX
<Button sx={{ '&.Mui-disabled': { opacity: 0.4 } }} disabled>Disabled</Button>
<TextField sx={{ '& .MuiInputBase-root': { borderRadius: 4 } }} />

Prefer theme overrides for global changes and sx for individual ones. Reaching into inner classes ties your code to MUI's internal structure, which can change between major versions, though the documented state classes are stable.

slots and slotProps

Complex components have several inner parts: a Dialog has a root, a backdrop and a paper; TextField has a label, an input and helper text. Current MUI versions expose these through slots (replace a part with another component) and slotProps (pass props to a part):

JSX
<Dialog
  open={open}
  slotProps={{
    paper: { sx: { borderRadius: 3 } },
    backdrop: { sx: { backdropFilter: 'blur(4px)' } },
  }}
/>

<Autocomplete
  slots={{ paper: MyCustomPaper }}
/>

Older code uses separate props such as PaperProps, InputProps and inputProps; they still work in many components but are being deprecated in favor of slotProps. When you read a tutorial, check whether it uses the old or new names, and check which your installed version supports.

Stand-in: slots

This stand-in card exposes two slots (header and action) in the same style as MUI: slots replaces the component, slotProps configures it.

Stand-in: slots and slotProps

Common mistakes

  • Passing a new inline component to component (component={(props) => <Link {...props} />}). It is re-created every render, so React remounts the element and you lose focus and state. Define the component outside, or use forwardRef-compatible stable references.
  • Forgetting that the component given to component must forward className and ref to a DOM element, or styling and focus break.
  • Using style where sx would pick up theme values, spacing and breakpoints.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the component prop do?

    It makes the component polymorphic: same styles and behavior, different root element, for example RouterLink or 'section'.
  2. 2. What happens to props that MUI does not use itself, such as data-testid?

    Unknown props are spread onto the root element, so id, aria-* and data-* attributes just work.
  3. 3. In a TextField, which prop gives access to the actual input element?

    ref points to the outer wrapper; inputRef points to the <input> itself.
  4. 4. What is the difference between slots and slotProps?

    slots swap the component used for a part, slotProps configure the part (old and new).

Summary

  • Most MUI components share sx, className, ref, component, variant, color, size and disabled.
  • Unknown props are forwarded to the root DOM element.
  • component changes the rendered element or component (links with a router, semantic tags).
  • Use slots and slotProps to replace or tune inner parts; older code uses PaperProps, InputProps and similar names.
  • Never create the component function inline during render.

Next: Box and Container