最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extends material-ui component - Stack Overflow

programmeradmin5浏览0评论

I want to exetend Tab ponent with es6 class like this:

import React from "react";

import {Tab} from "material-ui";

class MyTab extends Tab {
    constructor(props){
        super(props);
    }

    render(){
        return super.render();
    }

}

export default MyTab;

But i get an error:

Uncaught TypeError: Cannot read property 'muiTheme' of undefined

What am I doing wrong?

I want to exetend Tab ponent with es6 class like this:

import React from "react";

import {Tab} from "material-ui";

class MyTab extends Tab {
    constructor(props){
        super(props);
    }

    render(){
        return super.render();
    }

}

export default MyTab;

But i get an error:

Uncaught TypeError: Cannot read property 'muiTheme' of undefined

What am I doing wrong?

Share Improve this question edited Feb 14, 2016 at 12:03 B. Branchard asked Feb 11, 2016 at 9:40 B. BranchardB. Branchard 8551 gold badge13 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The correct way to extend an MUI ponent is using their withStyles() higher-order ponent Design Approach

import React, { Component } from "react";
import Tab from "@material-ui/core/Tab";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => {
  return ({
      myTab: {
        fontFamily: 'Courier New',
    });
}

class MyTab extends Component {

  render() {
    return (
      const {classes, ...other} = this.props
      <Tab {...other} className={classes.myTab} label="Home" />
   }
}

export default withStyles(styles)(MyTab); 

Currently it seems to require a 'getInitialState' function even if you are working in ES6. So just add one (and ignore the errors) for now.

import ThemeManager from 'material-ui/lib/styles/theme-manager';
import LightTheme from 'material-ui/lib/styles/raw-themes/light-raw-theme';

getInitialState() {
    return this.state;
}

I also set this state in the constructor ala ES6 React.

this.state = {muiThem : ThemeManager.getMuiTheme(LightThem),};
发布评论

评论列表(0)

  1. 暂无评论