前提・実現したいこと
https://qiita.com/junara/items/a4a98c27dc23fd53ebb9
このサイトを見ながらreact-routerのwithRouterを使って、onClickでのSPAページ遷移を作っています。
自分は、material-uiのappbarで発火させるようにしたいのですが、Routeの子の子の要素であるため、withRouterを使用してのページ遷移になると考えています。
しかし、同じようにしてもボタンをクリック後以下のようなエラーが発生してうまくいきません。
違いと言えば自分はfunctionコンポーネント内で記述していることです。
何か解決策があればコメントお願いします。
発生している問題・エラーメッセージ
Uncaught TypeError: Cannot read property 'props' of undefined
該当のソースコード
main.jsx
render( <BrowserRouter> <div> <MuiThemeProvider theme={theme}> <Switch> <Route exact path="/" component={DemoApp} /> <Route path="/sample" component={Sample} /> </Switch> </MuiThemeProvider> </div> </BrowserRouter>, document.getElementById('root') )
MenuAppBar.js
MenuAppBar.js
1function MenuAppBar() { 2//省略 3 4 const handleChange = event => { 5 setAuth(event.target.checked); 6 }; 7 8 const handleMenu = event => { 9 setAnchorEl_icon(event.currentTarget); 10 }; 11 12 const handleClose = () => { 13 setAnchorEl_icon(null); 14 setAnchorEl_menu(null); 15 }; 16 17 const handleClick = event => { 18 setAnchorEl_menu(event.currentTarget); 19 }; 20 21 const handleToPage = () => { 22 this.props.history.push('/sample') ///////////該当箇所 23 } 24 25 return ( 26 <div className={classes.root}> 27 <FormGroup> 28 <FormControlLabel 29 control={<Switch checked={auth} onChange={handleChange} aria-label="login switch" />} 30 label={auth ? 'Logout' : 'Login'} 31 /> 32 </FormGroup> 33 <AppBar position="static"> 34 <Toolbar> 35 <IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="menu" onClick={handleClick}> 36 <MenuIcon 37 aria-controls="customized-menu" 38 aria-haspopup="true" 39 variant="contained" 40 /> 41 </IconButton> 42 <StyledMenu 43 id="customized-menu" 44 anchorEl={anchorEl_menu} 45 keepMounted 46 open={open_menu} 47 onClose={handleClose} 48 > 49 <StyledMenuItem> 50 <ListItemIcon> 51 <PersonIcon fontSize="small" /> 52 </ListItemIcon> 53 <ListItemText primary="1" /> 54 </StyledMenuItem> 55 <StyledMenuItem onClick={handleToPage}> //////該当箇所 56 <ListItemIcon> 57 <CreateIcon fontSize="small" /> 58 </ListItemIcon> 59 <ListItemText primary="2" /> 60 </StyledMenuItem> 61 <StyledMenuItem> 62 <ListItemIcon> 63 <StoreIcon fontSize="small" /> 64 </ListItemIcon> 65 <ListItemText primary="3" /> 66 </StyledMenuItem> 67 </StyledMenu> 68 <Typography variant="h6" className={classes.title}> 69 sample 70 </Typography> 71 {auth && ( 72 <div> 73 <IconButton 74 aria-label="account of current user" 75 aria-controls="menu-appbar" 76 aria-haspopup="true" 77 onClick={handleMenu} 78 color="inherit" 79 > 80 <AccountCircle /> 81 </IconButton> 82 <Menu 83 id="menu-appbar" 84 anchorEl={anchorEl_icon} 85 anchorOrigin={{ 86 vertical: 'top', 87 horizontal: 'right', 88 }} 89 keepMounted 90 transformOrigin={{ 91 vertical: 'top', 92 horizontal: 'right', 93 }} 94 open={open_icon} 95 onClose={handleClose} 96 > 97 <MenuItem onClick={handleClose}>Profile</MenuItem> 98 <MenuItem onClick={handleClose}>My account</MenuItem> 99 </Menu> 100 </div> 101 )} 102 </Toolbar> 103 </AppBar> 104 </div> 105 ); 106} 107 108export default withRouter(MenuAppBar)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。