Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

Material-ui Cannot read property 'withStyles' of undefined error

Getting error:

Uncaught TypeError: Cannot read property 'withStyles' of undefined
at withStyles (withStyles.js:6)
at Module../node_modules/@material-ui/core/esm/SvgIcon/SvgIcon.js (SvgIcon.js:160)
at webpack_require (bootstrap:18)
at Module../node_modules/@skoda/k2tools-app-components/build/index.es.js (redux-saga-symbols.esm.js:39)
at webpack_require (bootstrap:18)
at Module../src/pages/error/errorPage.tsx (calculator.min.js?v=516:5549)
at webpack_require (bootstrap:18)
at Module../src/pages/error/index.ts (calculator.min.js?v=516:5590)
at webpack_require (bootstrap:18)
at Module../src/components/errorBoundary/errorBoundary.tsx (calculator.min.js?v=516:1903)

Expected Behavior ?? No error.

Context ?? We built an application which should be distribued as javascript file from our api. Client web downloads and loads this file (starts application as widget). Client web may or may not use material-ui, thus we added @material-ui/core in external dependencies, and client web should handle it yourselve by installing material-ui from npm, or linking it from cdn

in application webpack.config

    externals: {
        'react': {
            commonjs: 'react',
            commonjs2: 'react',
            amd: 'react',
            umd: 'react',
            root: 'React'
        },
        'react-dom': {
            commonjs: 'react-dom',
            commonjs2: 'react-dom',
            amd: 'react-dom',
            umd: 'react-dom',
            root: 'ReactDOM'
        },
        '@material-ui/core': {
            commonjs: '@material-ui/core',
            commonjs2: '@material-ui/core',
            amd: '@material-ui/core',
            umd: '@material-ui/core',
            root: 'MaterialUI'
        }
    }

Client web downloads this bundle and also links this cript

Or if client web has npm, installs material-ui and sets it in global variable like this

import * as MaterialUI from "@material-ui/core"; window.MaterialUI = MaterialUI; Everything was fine until we run into issue when all material icons colors and sizes were overriden by default pallete (all icons colors of client web was overriden to blue). This was solved by adding also @material-ui/styles module in external dependencies. Please see this issue https://github.com/mui-org/material-ui/issues/19702 for more information.

            '@material-ui/styles': {
                commonjs: '@material-ui/styles',
                commonjs2: '@material-ui/styles',
                amd: '@material-ui/styles',
                umd: '@material-ui/styles',
                root: 'MaterialUIStyles'
            }

It was working fine again with npm approach.

import * as MaterialUIStyles from "@material-ui/styles";
window.MaterialUIStyles = MaterialUIStyles;

But how should i add material-ui/styles in external dependencies to make it work also with cdn approach ?

I got an error Cannot read property 'withStyles' of undefined. Obviously material-ui/styles module is missing.

Am i specifiing root name wrong ? Or should i link material-ui/styles cdn from somewhere and from where and how ? I cannot find any usefull information in documentation about this context.

question from:https://stackoverflow.com/questions/65953137/material-ui-cannot-read-property-withstyles-of-undefined-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...