【问题标题】:What is the muiName property and when do I have to set it for Material-UI components?muiName 属性是什么?什么时候必须为 Material-UI 组件设置它?
【发布时间】:2017-08-30 10:50:11
【问题描述】:

在官方 material-ui 文档中,AppBar 组件 here 的示例如下所示:

import React, {Component} from 'react';
import AppBar from 'material-ui/AppBar';
import IconButton from 'material-ui/IconButton';
import IconMenu from 'material-ui/IconMenu';
import MenuItem from 'material-ui/MenuItem';
import FlatButton from 'material-ui/FlatButton';
import Toggle from 'material-ui/Toggle';
import MoreVertIcon from 'material-ui/svg-icons/navigation/more-vert';
import NavigationClose from 'material-ui/svg-icons/navigation/close';

class Login extends Component {
  static muiName = 'FlatButton';

  render() {
    return (
      <FlatButton {...this.props} label="Login" />
    );
  }
}

const Logged = (props) => (
  <IconMenu
    {...props}
    iconButtonElement={
      <IconButton><MoreVertIcon /></IconButton>
    }
    targetOrigin={{horizontal: 'right', vertical: 'top'}}
    anchorOrigin={{horizontal: 'right', vertical: 'top'}}
  >
    <MenuItem primaryText="Refresh" />
    <MenuItem primaryText="Help" />
    <MenuItem primaryText="Sign out" />
  </IconMenu>
);

Logged.muiName = 'IconMenu';

/**
 * This example is taking advantage of the composability of the `AppBar`
 * to render different components depending on the application state.
 */
class AppBarExampleComposition extends Component {
  state = {
    logged: true,
  };

  handleChange = (event, logged) => {
    this.setState({logged: logged});
  };

  render() {
    return (
      <div>
        <Toggle
          label="Logged"
          defaultToggled={true}
          onToggle={this.handleChange}
          labelPosition="right"
          style={{margin: 20}}
        />
        <AppBar
          title="Title"
          iconElementLeft={<IconButton><NavigationClose /></IconButton>}
          iconElementRight={this.state.logged ? <Logged /> : <Login />}
        />
      </div>
    );
  }
}

export default AppBarExampleComposition;

我的问题是关于陈述的

static muiName = 'FlatButton';

Logged.muiName = 'IconMenu';

什么是muiName,我何时/为什么必须设置它?是否应该始终设置为render() 方法中顶级组件的名称?

在同一网页上,AppBar 的示例未设置 muiName

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    documentation 中的答案是:

    In order to provide the maximum flexibility and performance, we need a way to know the nature of the child elements a component receives. To solve this problem we tag some of our components when needed with a muiName static property.

    让我们看看这个例子: // @flow 弱

    import React from 'react';
    import IconButton from 'material-ui/IconButton';
    import Icon from 'material-ui/Icon';
    
    const WrappedIcon = props => <Icon {...props} />;
    WrappedIcon.muiName = 'Icon';
    
    export default function Composition() {
      return (
        <div>
          <IconButton>
            <Icon>alarm</Icon>
          </IconButton>
          <IconButton>
            <WrappedIcon>alarm</WrappedIcon>
          </IconButton>
        </div>
      );
    }
    

    如果您包装了 material-ui 组件,您应该将 'muiName' 属性设置为包装器组件,其值为您包装的 material-ui 组件的名称。 我希望你能理解这句话:)

    【讨论】:

    • “当需要时”是什么意思?我应该如何知道何时以及将属性设置为什么?就像提到的那样:有些例子可以做到,有些则没有。附带说明:您已经链接了测试版 v1.0.0-beta.6 的文档。我应该提到我使用的是最新的官方版本0.19.0。在那个文档中没有提到muiName,我错过了。对不起。
    • @Joerg 我认为这个具有 muiName 静态属性的功能将包含在下一个主要版本中。如果您检查我之前提到的链接,您可以找到带有答案的示例。我会尝试更新我的答案向您解释。
    • 感谢您详细说明示例。所以“当需要时”翻译成“总是”?那么为什么其他例子不设置muiName呢?它们都通过箭头函数定义 React 组件,因此以一种或另一种方式包装 Material-UI 组件。但是,它们不设置muiName。我还是不明白。
    • 如果您专注于应用程序的性能,最好信任 material-ui 的维护者并为 material-ui 组件的所有包装器设置 muiName...或阅读 material-ui 的代码并尝试找到muiName 使用和挖掘的地方。
    • 好的,但是问题又来了,为什么那些维护者自己没有在他们自己的例子中设置muiName
    【解决方案2】:

    documentation 中的例子对我帮助很大。

    现在使用v4.8.1,您可以添加muiName,如下所示:

    const WrappedIcon = props => <Icon {...props} />;
    WrappedIcon.muiName = Icon.muiName;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 2021-04-01
      • 2011-11-28
      • 1970-01-01
      • 2011-01-21
      • 2014-11-08
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多