Cannot read property 'prepareStyles' of undefined

前端 未结 2 1670
北海茫月
北海茫月 2021-01-12 11:28

I am trying to open a Dialog box by a button click. When I am clicking the button the Dialog first of all is not opened and I am getting Error :

相关标签:
2条回答
  • 2021-01-12 11:46

    I don't have enough rep to comment on Mayank's answer but they are correct. To further elaborate on Maynak's answer, you only need to add <MuiThemeProvider></<MuiThemeProvider> to the main app container. If you do that, you should never have to worry about adding it anywhere else in your app.

    Note the parent component on the left and the child component in this image:

    0 讨论(0)
  • 2021-01-12 11:54

    All the material-ui component must be rendered inside <MuiThemeProvider></MuiThemeProvider> tag, so we need to wrap topmost component (or at least any parent component) in material-ui's MuiThemeProvider component.


    Issue is, your Dialog is outside of the MuiThemeProvider tag, put dialog also inside it, it should work.

    Write it like this:

        <div>
            <MuiThemeProvider muiTheme={muiThemebtn}>
                <RaisedButton label={Lang.AddUser} 
                  onTouchTap={this.openModal}
                  primary={true}
                  display='none'
                  icon={<ContentAddBox color={darkBlack} style={{backgroundColor:'#e3e3e3'}}/>}
                />
               <Dialog
                 title="Scrollable Dialog"
                 actions={actions}
                 modal={false}
                 open={this.state.open}
                 onRequestClose={this.handleClose}
                 autoScrollBodyContent={true}
               >
                 Dialog Text
              </Dialog>
           </MuiThemeProvider>
        </div>
    

    Suggestion:

    If you are using material ui elements in many components, then no need to put MuiThemeProvider tag on each page instead of that you can put in you homepage or better to put in index.js page, where we used to define all the routes, like this:

    const muiThemebtn = getMuiTheme()   
    
    ReactDOM.render((
      <MuiThemeProvider muiTheme={muiThemebtn}>
          <Router history={hashHistory}>
              <Route path="/" component={comp1}>
                <Route path="/abc" component={comp2}/>
              </Route>
          </Router>
      </MuiThemeProvider>
    ), document.getElementById('app'));
    
    0 讨论(0)
提交回复
热议问题