onLeftIconButtonTouchTap

onLeftIconButtonTouchTap

本文介绍了为什么Material-UI Appbar onLeftIconButtonTouchTap不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Material-UI的React-Redux.
现在,我正在尝试创建示例应用,但无法正常工作.
我不知道如何改善我的代码.
当单击Material-UI AppBar onLeftIconButtonTouchTap时,我想打开Drawer.
我认为我无法将Action绑定到Component.
当以下代码运行时,不会触发onLeftIconButtonTouchTap事件.
当我将openDrawer更改为openDrawer()时,在Chrome开发工具中发现了JS错误"openDrawer不是函数".

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作为组件

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

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

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;

谢谢.

推荐答案

您没有正确绑定该函数.您需要将代码更新为

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

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

此处有关.bind()方法的更多信息:使用JavaScript的绑定"方法

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

这篇关于为什么Material-UI Appbar onLeftIconButtonTouchTap不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 05:44