Why Material-UI Appbar onLeftIconButtonTouchTap is not work?

Im studying React-Redux, Material-UI.
Now, Im trying to create Sample App, but not work.
I dont know how to improve my code.
I want to open Drawer , when Material-UI AppBar onLeftIconButtonTouchTap is clicked.
I cant bind my Action to Component, I think.
When following code is running, not fire onLeftIconButtonTouchTap event.
And when I change openDrawer to openDrawer(), JS error 'openDrawer is not a function' is found on Chrome Dev tool.

Header.jsx as a Component

import React, { PropTypes } from 'react';
import AppBar from 'material-ui/AppBar';
import Drawer from 'material-ui/Drawer';

const Header = ({
  openDrawer,
  open
}) => (
  <div>
    <AppBar
      title='sample'
      onLeftIconButtonTouchTap = {() => openDrawer}
    />
    <Drawer 
      docked={false}
      open={open}
    />
  </div>
)

Header.PropTypes = {
  openDrawer: PropTypes.func.isRequired,
  open: PropTypes.bool.isRequired
}

export default Header;

HeaderContainer.jsx as a Container

import React from 'react';
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux';

import { header } from '../actions'
import Header from '../components/Header';

const mapStateToProps = (state) => ({open});
const mapDispatchToProps = (dispatch) => (
  {openDrawer: bindActionCreators(header, dispatch)}
);

const HeaderContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(Header);

export default HeaderContainer;

App.jsx as a RootComponent

import React, { Component, PropTypes } from 'react';

import Header from '../components/Header';

import injectTapEventPlugin from "react-tap-event-plugin";

import baseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import getMuiTheme from 'material-ui/styles/getMuiTheme';

injectTapEventPlugin();

class App extends Component {
  constructor(props) {
    super(props);
  }

 getChildContext() {
      return { muiTheme: getMuiTheme(baseTheme) };
  }

  render() {
    return (
      <div>
        <Header />
        {this.props.children}
      </div>
    );
  }
};

App.childContextTypes = {
    muiTheme: React.PropTypes.object.isRequired,
};

export default App;

Thanks in advance.

Answers:

Answer

You are not binding the function correctly. You need to update your code to

<AppBar
  title='sample'
  onLeftIconButtonTouchTap = { openDrawer.bind(this) }
/>

More about .bind() method here: Use of the JavaScript 'bind' method

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.