【问题标题】:Material UI - muiTheme.prefix is not a function issueMaterial UI - muiTheme.prefix 不是功能问题
【发布时间】:2016-04-20 11:25:13
【问题描述】:

我最近从 v0.14.4 迁移到 Google Material UI (Material UI v0.15.0-beta.1),由于最新的 reactjs v15.0.1,还升级了 formsy-material-ui 和 formsy-react 包装器表单验证。

这是一个大代码架构,抱歉无法附上代码 sn-p,反正我收到一个错误,上面写着 muiTheme.prefix is not a function

虽然我一直遵循https://github.com/callemall/material-ui/blob/master/CHANGELOG.md 上的材料 ui 更改日志文档提供的指南

希望得到一些帮助,如果需要更好地解释问题,请告诉我。

【问题讨论】:

    标签: reactjs material-ui formsy-material-ui formsy-react


    【解决方案1】:

    遇到了问题,我错过了要在 getMuiTheme 方法中传递的主题上下文 lightBaseTheme,如下所示。

      getChildContext() {
        return {muiTheme: getMuiTheme(lightBaseTheme)};
      }
    

    【讨论】:

      【解决方案2】:

      我猜你没有对这个重大变化做任何事情:

      到目前为止,您需要根据上下文提供主题,请参阅:http://www.material-ui.com/#/customization/themes

      所以如果您不指定主题,material-ui 现在似乎会中断。您可以将整个应用程序包装在 MuiThemeProvider 中并使其恢复工作。

      你需要类似的东西:

      ...
      import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"
      import getMuiTheme from "material-ui/styles/getMuiTheme"
      // import the colors wanted to customize your theme here, if you want to
      import { orange500 } from "material-ui/styles/colors"
      ...
      // customize your theme here
      const muiTheme = getMuiTheme({
        palette: {
          accent1Color: orange500
        }
      })
      ...
      class MyApp extends Component {
      
      
        render() {
          return (
            <MuiThemeProvider muiTheme={muiTheme}>
              <div>
                <Header />
                <Body />
                <Footer />
              </div>
            </MuiThemeProvider>
          )
        }
      }
      

      需要注意的一点是 MuiThemeProvider 只需要一个孩子,因此无法避免封装 div;当然,如果你想让事情变得更干净,你可以创建另一个 AppContainer 组件,但你明白了。

      【讨论】:

      • 感谢约翰尼的意见,我已经这样做了,但即便如此仍然遇到同样的问题。我的假设是,所有子组件都不需要用MuiThemeProvider 包裹,因为它应该从父组件继承上下文。
      • 您使用的是哪些组件?我在使用下拉菜单时看到了这个未解决的问题:github.com/callemall/material-ui/issues/3756 如果你也使用它,而且我知道你有很多代码,但你可以尝试用其他东西替换那个组件吗?确认或揭穿此错误与此有关。
      • 我确实看到了这个问题,你也可以看到我的 +1 :) 但这是相当老的一个,似乎与我们的无关。它将出现在所有页面上,接受第一个。
      猜你喜欢
      • 2020-06-17
      • 2018-10-06
      • 2019-09-20
      • 2018-12-02
      • 2017-08-19
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多