gpt4 book ai didi

reactjs - react -material-ui appbar图标触摸事件不触发

转载 作者:行者123 更新时间:2023-12-02 12:14:33 25 4
gpt4 key购买 nike

当我单击元素 AppBar(左侧的图标)时,应该执行 _handleClick() 方法。我无法收到控制台消息。我使用的是material-ui框架,并且属性onLeftIconButtonTouchTap是为通过触摸点击选择左侧图标时的回调函数提供的。

import React, { Component } from 'react'
import { AppBar, IconButton } from 'material-ui'
import MoreVertIcon from 'material-ui/lib/svg-icons/navigation/more-vert';

let injectTapEventPlugin = require("react-tap-event-plugin");

//Needed for onTouchTap
//Can go away when react 1.0 release
//Check this repo:
//https://github.com/zilverline/react-tap-event-plugin
injectTapEventPlugin();


class Header extends Component {
constructor(props) {
super(props);
this._handleClick = this._handleClick.bind(this);
}

_handleClick(e) {
e.preventDefault();
// Show/Hide the LeftMenu
window.console.log("Click!");
}

render() {
return (
<AppBar title="Arasaaccc"
iconElementLeft={ <IconButton>
<MoreVertIcon/>
</IconButton> }
onLeftIconButtonTouchTap={ this._handleClick }
isInitiallyOpen={ true } />


)
}
}

export default Header

但是它可以与另一个组件一起使用:

class Prueba extends Component {
constructor(props) {
super(props);
this._handleClick = this._handleClick.bind(this);
}

_handleClick(e) {
e.preventDefault();
window.console.log("Click!");
}
render (){
return (
<h1 onClick={this._handleClick}>Prueba Prueba Prueba</h1>
)
}
}
export default Prueba;

最佳答案

如果为 AppBar 组件指定图标,onLeftIconButtonTouchTap 事件将不起作用。您不指定图标:

<AppBar title="Arasaaccc" 
onLeftIconButtonTouchTap={ this._handleClick }
isInitiallyOpen={ true } />

或者您在 IconButton 组件上应用事件:

<AppBar title="Arasaaccc"
iconElementLeft={ <IconButton onTouchTap={ this._handleClick } >
<MoreVertIcon />
</IconButton> }
isInitiallyOpen={ true } />

编辑:请注意,根据此 GitHub issue ,问题应该解决。您仍然无法在 iconElementLeftonLeftIconButtonTouchTap 中同时使用 _handleClick,无论是其中之一。

关于reactjs - react -material-ui appbar图标触摸事件不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34223966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com