質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

2回答

2311閲覧

withRouterを使ったページ遷移がうまく行かない

aiai8976

総合スコア112

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2020/01/28 08:34

編集2020/01/28 08:41

前提・実現したいこと

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

そもそもappbar自体をBrowserRouter直下にしたらいいだけの話でした。

投稿2020/01/28 11:53

aiai8976

総合スコア112

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

function component ではthis.propsのような使い方が出来ないだけではないでしょうか

function MenuAppBar(props) { //省略 const handleToPage = () => { props.history.push('/sample')  ///////////該当箇所 } //省略

投稿2020/01/28 08:43

tutti56

総合スコア86

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aiai8976

2020/01/28 08:44

無くしてみましたが、エラーのままでした。
aiai8976

2020/01/28 08:46

エラー内容をよく見るとpushが定義されていないに変わっていました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問