前提・実現したいこと
React+Redux+Material-UIを用いて簡単なアプリケーションを作成しています。
Material-UIのDrawerのopen,closeをReduxで管理しているのですが、ボタンを押しても一向に開いてくれません。
どなたか解決方法のご教授をお願い致します。
該当のソースコード
/** * components/Header.js */ const drawerWidth = 200 // Width for Drawer const styles = theme => ({ root: { flexGrow: 1, zIndex: 1, overflow: 'hidden', position: 'relative', display: 'flex', width: '100%', }, appBar: { position: 'absolute', marginLeft: drawerWidth, [theme.breakpoints.up('md')]: { width: `calc(100% - ${drawerWidth}px)`, }, }, toolbar: theme.mixins.toolbar, navIconHide: { [theme.breakpoints.up('md')]: { display: 'none', }, }, drawerPaper: { width: drawerWidth, [theme.breakpoints.up('md')]: { position: 'relative', }, }, drawerListSubheader: { backgroundColor: theme.palette.background.paper, }, content: { flexGrow: 1, overflowY: 'scroll', backgroundColor: theme.palette.background.default, padding: theme.spacing(3), }, }) class Main extends React.Component { render() { const { classes, theme } = this.props const drawer = ( <div> <List> <ListSubheader className={classes.drawerListSubheader} component="div">First List</ListSubheader> <ListItem button> <ListItemText primary="First List Button" /> </ListItem> </List> <Divider /> <List> <ListSubheader className={classes.drawerListSubheader} component="div">Second List</ListSubheader> <ListItem button> <ListItemText primary="Second List Button" /> </ListItem> </List> </div> ) return ( <div className={classes.root}> <AppBar className={classes.appBar}> <Toolbar> <IconButton color="inherit" // アイコンの色 aria-label="open drawer" // ラベル付け onClick={() => this.props.handleToggleDrawer()} className={classes.navIconHide} > <MenuIcon /> {/* メニューアイコン */} </IconButton> <Typography color="inherit" noWrap> Suggesh </Typography> </Toolbar> </AppBar> <Hidden mdUp> {/* md未満 */} <Drawer variant="temporary" anchor={theme.direction === 'rtl' ? 'right' : 'left'} open={this.props.mobileOpen} onClose={() => this.props.handleToggleDrawer()} classes={{ paper: classes.drawerPaper, }} ModalProps={{ keepMounted: true, // Better open performance on mobile. }} > {drawer} </Drawer> </Hidden> <Hidden smDown implementation="css"> {/* md以上 */} <Drawer variant="permanent" open classes={{ paper: classes.drawerPaper, }} > {drawer} </Drawer> </Hidden> <main className={classes.content}> <div className={classes.toolbar} /> <h1>Hello World</h1> </main> </div> ) } } Main.propTypes = { classes: PropTypes.object.isRequired, theme: PropTypes.object.isRequired, } export default withStyles(styles, { withTheme: true })(Main) /** * containers/HeaderContainer.js /* import { connect } from 'react-redux'; import Header from '../components/Header' import { toggleDrawer } from '../actions/HeaderActions' const mapStateToProps = state => ({ mobileOpen: state.mobileOpen }) const mapDispatchToProps = dispatch => ({ handleToggleDrawer: () => dispatch(toggleDrawer()) }) export default connect(mapStateToProps, mapDispatchToProps)(Header) /** * reducers/HeaderDrawerReducer.js /* import * as actionTypes from '../utils/actionTypes' const initialState = { mobileOpen: false, } export default function HeaderDrawerReducer(state = initialState, action) { switch (action.type) { case actionTypes.TOGGLE_DRAWER: console.log('mobileOpen: ' + !state.mobileOpen) return Object.assign({}, state, { mobileOpen: !state.mobileOpen, }) default: return state } } /** * reducers/index.js /* import { combineReducers } from 'redux'; import HeaderDrawerReducer from './HeaderDrawerReducer'; const reducers = combineReducers({ HeaderDrawerReducer: HeaderDrawerReducer, }); export default reducers; /** * utils/actionTypes.js */ export const TOGGLE_DRAWER = 'TOGGLE_DRAWER'; **ーーー追記ーーー** /** * actions/HeaderActions.js */ import { TOGGLE_DRAWER } from "../utils/actionTypes"; export function toggleDrawer(){ return { type: TOGGLE_DRAWER } } /** * App.js */ import React from 'react'; import CssBaseline from '@material-ui/core/CssBaseline'; import {createStore} from 'redux'; import {Provider} from 'react-redux'; import HeaderContainer from './containers/HeaderContainer'; import reducers from './reducers/index'; const store = createStore(reducers) function App() { return ( <Provider store={store}> <CssBaseline> <HeaderContainer /> </CssBaseline> </Provider> ); } export default App; /** * index.js */ import React from 'react' import ReactDOM from 'react-dom' import App from './App' ReactDOM.render( <App />, document.getElementById('root') )
試したこと
エラーはありません。
また、コンソールにはしっかりmobileOpenの値が出力されています。
ーー追記ーー
App.js で reducers/index.js を読み込むのをやめて、直接reducers/HeaderDrawerReducers を読み込めば適切に動作しました。
回答1件
あなたの回答
tips
プレビュー