Migration from v3 to v4 - Material UI (original) (raw)

Yeah, v4 has been released!

Looking for the v3 docs? You can find the latest version here.

Introduction

This is a reference for upgrading your site from Material UI v3 to v4. While there's a lot covered here, you probably won't need to do everything for your site. We'll do our best to keep things easy to follow, and as sequential as possible so you can quickly get rocking on v4!

Why you should migrate

This documentation page covers the how of migrating from v3 to v4. The why is covered in the release blog post on Medium.

Updating your dependencies

The very first thing you will need to do is to update your dependencies.

Update Material UI version

You need to update your package.json to use the latest version of Material UI.

"dependencies": {
  "@material-ui/core": "^4.0.0"
}

Or run

npm install @material-ui/core

or

yarn add @material-ui/core

Update React version

The minimum required version of React was increased from react@^16.3.0 to react@^16.8.0. This allows us to rely on Hooks (we no longer use the class API).

Update Material UI Styles version

If you were previously using @material-ui/styles with v3 you need to update your package.json to use the latest version of Material UI Styles.

"dependencies": {
  "@material-ui/styles": "^4.0.0"
}

Or run

npm install @material-ui/styles

or

yarn add @material-ui/styles

Handling breaking changes

Core

Styles

-const DeepChild = withTheme()(DeepChildRaw);  
+const DeepChild = withTheme(DeepChildRaw);  
-import { convertHexToRgb } from '@material-ui/core/styles/colorManipulator';  
+import { hexToRgb } from '@material-ui/core/styles';  
  rippleVisible: {  
    opacity: 0.3,  
-   animation: 'mui-ripple-enter 100ms cubic-bezier(0.4, 0, 0.2, 1)',  
+   animation: '$mui-ripple-enter 100ms cubic-bezier(0.4, 0, 0.2, 1)',  
  },  
  '@keyframes mui-ripple-enter': {  
    '0%': {  
      opacity: 0.1,  
    },  
    '100%': {  
      opacity: 0.3,  
    },  
  },  

Theme

-const background = { main: color };  
-theme.palette.augmentColor(background);  
+const background = theme.palette.augmentColor({ main: color });  
 console.log({ background });  
 typography: {  
-  useNextVariants: true,  
 },  
 label: {  
   [theme.breakpoints.up('sm')]: {  
-    paddingTop: theme.spacing.unit * 12,  
+    paddingTop: theme.spacing(12),  
   },  
 }  

Tip: you can provide more than 1 argument: theme.spacing(1, 2) // = '8px 16px'.
You can use the migration helper on your project to make this smoother.

Layout

  /**  
   * Defines the space between the type `item` component.  
   * It can only be used on a type `container` component.  
   */  
-  spacing: PropTypes.oneOf([0, 8, 16, 24, 32, 40]),  
+  spacing: PropTypes.oneOf([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),  

Going forward, you can use the theme to implement a custom Grid spacing transformation function.

-import Container from '@material-ui/lab/Container';  
+import Container from '@material-ui/core/Container';  

TypeScript

value type

Normalized value prop type for input components to use unknown. This affectsInputBase, NativeSelect, OutlinedInput, Radio, RadioGroup, Select, SelectInput, Switch, TextArea, and TextField.

 function MySelect({ children }) {
-  const handleChange = (event: any, value: string) => {
+  const handleChange = (event: any, value: unknown) => {
     // handle value
   };

   return <Select onChange={handleChange}>{children}</Select>
 }

This change is explained in more detail in the TypeScript guide

Button

-<Button variant="raised" />  
+<Button variant="contained" />  
-<Button variant="flat" />  
+<Button variant="text" />  
-import Button from '@material-ui/core/Button';  
-<Button variant="fab" />  
+import Fab from '@material-ui/core/Fab';  
+<Fab />  
-import Button from '@material-ui/core/Button';  
-<Button variant="extendedFab" />  
+import Fab from '@material-ui/core/Fab';  
+<Fab variant="extended" />  

Card

ClickAwayListener

Dialog

Divider

-<Divider inset />  
+<Divider variant="inset" />  

ExpansionPanel

List

Paper

-<Paper />  
+<Paper elevation={2} />  

This affects the ExpansionPanel as well.

Portal

Slide

Slider

-import Slider from '@material-ui/lab/Slider'  
+import Slider from '@material-ui/core/Slider'  

Switch

Snackbar

SvgIcon

-<AddIcon nativeColor="#fff" />  
+<AddIcon htmlColor="#fff" />  

Tabs

-<Tabs fullWidth scrollable />  
+<Tabs variant="scrollable" />  

Table

-<TableCell numeric>{row.calories}</TableCell>  
+<TableCell align="right">{row.calories}</TableCell>  
-<TableCell padding="dense" />  
+<TableCell size="small" />  

TextField

 <InputLabel  
-  FormLabelClasses={{ asterisk: 'bar' }}  
+  classes={{ asterisk: 'bar' }}  
 >  
   Foo  
 </InputLabel>  

Tooltip

Typography

-<Typography headlineMapping={headlineMapping}>  
+<Typography variantMapping={variantMapping}>  

Node

UMD

 const {  
   Button,  
   TextField,  
-} = window['material-ui'];  
+} = MaterialUI;  

It's consistent with other React projects: