跳转到内容

从v0.x版本迁移到v1版本

是的,v1版本已经发布了! 我们用了两年的努力达到了这个里程碑。

常问问题

哇—— API 看起来完全不一样! 这是否意味着1.0完全不同,而我得重新学习基础知识,而迁移是几乎不可能的?

我很高兴你问了! 答案是不。我们的核心概念并没有改变。 You will notice that the API provides more flexibility, but this has a cost – lower-level components that abstract less complexity.

到底是什么带来了如此巨大的改变呢?

Material-UI 这个项目是从4年前开始的。 在此期间,整个个生态系统发展了很多,我们也学到了很多东西。 @nathanmarks 启动了一项雄心勃勃的任务,将 Material-UI 重新启动,并利用我们学到的知识,来解决一些长期存在的问题。 譬如这些主要的变化:

我应该从哪里开始迁移?

  1. 首先,和v0.x版本一起,安装v1.x版本的 Material-UI。

    用 yarn:

  yarn add material-ui
  yarn add @material-ui/core

Or with npm:

  npm install material-ui
  npm install @material-ui/core

then

  import FlatButton from 'material-ui/FlatButton'; // v0.x
  import Button from '@material-ui/core/Button'; // v1.x
  1. Run the migration helper on your project. 3。 MuiThemeProvider is optional for v1.x., but if you have a custom theme, you are free to use v0.x and v1.x versions of the component at the same time, like this:

    import React from 'react';
    import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles'; // v1.x
    import { MuiThemeProvider as V0MuiThemeProvider} from 'material-ui';
    import getMuiTheme from 'material-ui/styles/getMuiTheme';
    
    const theme = createMuiTheme({
     /* theme for v1.x */
    });
    const themeV0 = getMuiTheme({
     /* theme for v0.x */
    });
    
    function App() {
     return (
       <MuiThemeProvider theme={theme}>
         <V0MuiThemeProvider muiTheme={themeV0}>
           {/*Components*/}
         </V0MuiThemeProvider>
       </MuiThemeProvider>
     );
    }
    
    export default App;
  2. 之后,您可以自由地一次迁移一个组件实例。

Components

Autocomplete

Material-UI doesn't provide a high-level API for solving this problem. You're encouraged you to explore the solutions the React community has built.

In the future, we will look into providing a simple component to solve the simple use cases: #9997.

Svg Icon

Run the migration helper on your project.

This will apply a change such as the following:

-import AddIcon from 'material-ui/svg-icons/Add';
+import AddIcon from '@material-ui/icons/Add';

<AddIcon />

Flat Button(扁平按钮)

-import FlatButton from 'material-ui/FlatButton';
+import Button from '@material-ui/core/Button';

-<FlatButton />
+<Button />

Raised Button(凸起的按钮)

凸起按钮的升级的路径:

-import RaisedButton from 'material-ui/RaisedButton';
+import Button from '@material-ui/core/Button';

-<RaisedButton />
+<Button variant="contained" />

Subheader(副标题)

-import Subheader from 'material-ui/Subheader';
+import ListSubheader from '@material-ui/core/ListSubheader';

-<Subheader>Sub Heading</Subheader>
+<ListSubheader>Sub Heading</ListSubheader>

Toggle(切换)

-import Toggle from 'material-ui/Toggle';
+import Switch from '@material-ui/core/Switch';

-<Toggle

-  toggled={this.state.checkedA}
-  onToggle={this.handleToggle}
-/>
+<Switch
+  checked={this.state.checkedA}
+  onChange={this.handleSwitch}
+/>

Menu Item(菜单项)

-import MenuItem from 'material-ui/MenuItem';
+import MenuItem from '@material-ui/core/MenuItem';

-<MenuItem primaryText="Profile" />
+<MenuItem>Profile</MenuItem>

Font Icon(字体图标)

-import FontIcon from 'material-ui/FontIcon';
+import Icon from '@material-ui/core/Icon';

-<FontIcon>home</FontIcon>
+<Icon>home</Icon>

Circular Progress(环状进度条)

-import CircularProgress from 'material-ui/CircularProgress';
+import CircularProgress from '@material-ui/core/CircularProgress';

-<CircularProgress mode="indeterminate" />
+<CircularProgress variant="indeterminate" />

Drop Down Menu(下拉式菜单)

-import DropDownMenu from 'material-ui/DropDownMenu';
+import Select from '@material-ui/core/Select';

-<DropDownMenu></DropDownMenu>
+<Select value={this.state.value}></Select>

未完待续...

您是否已成功迁移您的应用,并助社区一臂之力? There is an open issue in order to finish this migration guide #7195. 我们欢迎任何 pull request。