現在、rails webpackerにてmaterial-uiを使ってSPAの開発をしています。
material-ui公式のドキュメント(Full-height navigation)
と
Qiitaの記事(React: Material-UIのwithStyles()でCSSをJavaScriptコードに定める)
をもとに、以下のようにコードを書きました。
javascript
1import React, { Component } from 'react' 2import PropTypes from 'prop-types' 3import { render } from 'react-dom' 4import { BrowserRouter as Router, Route, withRouter } from 'react-router-dom' 5import { makeStyles } from '@material-ui/core/styles' 6import Drawer from '@material-ui/core/Drawer' 7import AppBar from '@material-ui/core/AppBar' 8import CssBaseline from '@material-ui/core/CssBaseline' 9import Toolbar from '@material-ui/core/Toolbar' 10import List from '@material-ui/core/List' 11import Typography from '@material-ui/core/Typography' 12import Divider from '@material-ui/core/Divider' 13import ListItem from '@material-ui/core/ListItem' 14import ListItemIcon from '@material-ui/core/ListItemIcon' 15import ListItemText from '@material-ui/core/ListItemText' 16import InboxIcon from '@material-ui/icons/MoveToInbox' 17import MailIcon from '@material-ui/icons/Mail' 18import { withStyles, MuiThemeProvider } from '@material-ui/core/styles' 19import About from './About' 20import Home from './Home' 21 22const drawerWidth = 240 23 24const styles = (theme) => ({ 25 root: { 26 display: 'flex', 27 }, 28 appBar: { 29 zIndex: theme.zIndex.drawer + 1, 30 }, 31 drawer: { 32 width: drawerWidth, 33 flexShrink: 0, 34 }, 35 drawerPaper: { 36 width: drawerWidth, 37 }, 38 content: { 39 flexGrow: 1, 40 padding: theme.spacing(3), 41 }, 42 toolbar: theme.mixins.toolbar, 43}); 44 45class App extends Component { 46 cpHome = () => { 47 this.props.history.push('/') 48 } 49 50 cpAbout = () => { 51 this.props.history.push('/About') 52 } 53 54 render() { 55 return ( 56 <div className={this.props.classes.root}> 57 <CssBaseline /> 58 <AppBar position="fixed" className={this.props.classes.appBar}> 59 <Toolbar> 60 <Typography variant="h6" noWrap> 61 Clipped drawer 62 </Typography> 63 </Toolbar> 64 </AppBar> 65 <Drawer 66 className={this.props.classes.drawer} 67 variant="permanent" 68 classes={{ 69 paper: this.prpos.classes.drawerPaper, 70 }} 71 > 72 <div className={this.props.classes.toolbar} /> 73 <List> 74 <ListItem button onClick={this.cpHome}> 75 <ListItemText primary={'Home'} /> 76 </ListItem> 77 <ListItem button onClick={this.cpAbout}> 78 <ListItemText primary={'About'} /> 79 </ListItem> 80 </List> 81 </Drawer> 82 <main className={this.props.classes.content}> 83 <Router> 84 <div> 85 <Route exact path='/' component={Home}/> 86 <Route path='/About' render={ () => <About name='shoya' /> }/> 87 </div> 88 </Router> 89 </main> 90 </div> 91 ) 92 } 93} 94 95export default withStyles(styles)(withRouter(App)) 96 97render( 98 <App />, 99 document.getElementById('app') 100);
がしかし、実際に記述したコードを実行すると
Cannot read property 'root' of undefined
とコンソールに表示されます。
試しに、上記のコードから
className={this.props.classes.root}
の記述を削除して実行してみたところ、
Cannot read property 'appBar' of undefined
と表示されます。(おそらくthis.props.classesに問題がある?classes自体がundefinedなのだろうか?)
material-uiは日本語のドキュメントも少なく、とても苦戦しております...。
ぜひお力添えお願いします。
足りない情報があれば追記します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/18 17:32
2019/08/18 17:40
2019/08/18 17:44
2019/08/18 17:53
2019/08/18 17:59
2019/08/18 18:00
2019/08/18 18:04
2019/08/19 04:52
2019/08/19 05:00
2019/08/19 05:05