为什么Material-UI Appbar onLeftIconButtonTouchTap不起作用?

时间:2017-03-01 16:52:10

标签: javascript reactjs redux material-ui

我正在研究React-Redux,Material-UI 现在,我试图创建示例应用程序,但不工作 我不知道如何改进我的代码 当点击了Material-UI AppBar onLeftIconButtonTouchTap时,我想打开抽屉。
我认为,我无法将我的行动绑定到组件 当以下代码运行时,不会触发onLeftIconButtonTouchTap事件 当我将openDrawer更改为openDrawer()时,在Chrome Dev工具上找到JS错误'openDrawer is not function'。

Header.jsx作为组件

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作为容器

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作为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;

提前致谢。

1 个答案:

答案 0 :(得分:1)

您没有正确绑定该功能。您需要将代码更新为

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

有关.bind()方法的更多信息,请访问Use of the JavaScript 'bind' method