在使用 react 和 material-ui 动态创建菜单项时,为什么 onTouchStart 会自动执行功能
While Dynamically Creating menu items with react and material-ui why does onTouchStart auto executes function
第一次发帖,我很高兴地说我终于有值得问的东西了!我要求进一步拓宽我对JavaScript
React
的理解,我感谢所有的反馈。
我目前正在编写一些前端代码,并且已经创建了一个带有material-ui
的leftNav。在创建菜单时,我想我会测试我对 javascript 和 react 的理解,并努力编写一个函数来创建菜单,而不必一遍又一遍地用 jsx <MenuItem props={}> </MenuItem>
编写,这很简单,我很高兴能让它工作。这是 react 组件内部的render
方法:
<LeftNav className={'animated ' + this.state.animation}
open={this.props.open}
style={style.leftNav}
width={220}
key={0}>
<Card style={style.card}>
<CardHeader avatar={null}
style={style.cardHeader}
zDepth={2}>
<Avatar size={65}
style={style.avatar}
src={avatarUrl}
onClick={ this._handleOpenSnackbar}/>
</CardHeader>
</Card>
<br/>
{this._createMenu() }
</LeftNav>
{this._createMenu() }
将创建我需要的所有MenuItem
,一切似乎都很完美,直到我需要为每个项目 (obv) 添加点击事件处理程序。如果我在 jsx 中<MenuItem props={}> </MenuItem>
堆叠在一起会更简单。
我刚刚添加了一个开关菜单来测试属性,但现在 onTouchTap={_routeHandler.call(prop)}
executing
最后一个测试用例并自动路由,而不仅仅是单击时路由。在我看来,我认为每个MenuItem
都有自己的点击事件处理程序,它们确实如此,但是尽管break
,但最后一个测试用例总是被执行。
我通过返回一个匿名函数解决了这个问题,然后该函数根据传递到_routeHandler()
上下文中的 prop 执行this
我要问的是,为什么需要开关大小写来返回一个匿名函数才能使onTapTouch
事件正常工作?我认为 onTapTouch 只有在单击MenuItem
时才会触发。
我很困惑,并假设 React 的一些内部工作原理我还没有完全理解。
下面是动态创建菜单的method
。
欢迎任何反馈。
谢谢
_createMenu = () => {
let menuOptionsAndIcons = {
'About me': <i className="fa fa-male" style={style.icons}/>,
'Skills': <i className="fa fa-certificate" style={style.icons}/>,
'Music': <i className="fa fa-music" style={style.icons}/>,
'Interests': <i className="fa fa-cogs" style={style.icons}/>
},
menu = [];
function _routeHandler() {
switch (this) {
case 'About me':
return function () {
if (typeof window !== 'undefined')
window.location.href = '' + 'http://' + window.location.host + '/about_me';
};
break;
case 'Skills':
return function () {
if (typeof window !== 'undefined')
window.location.href = '' + 'http://' + window.location.host + '/skills';
};
break;
case 'Music':
return function () {
if (typeof window !== 'undefined')
window.location.href = '' + 'http://' + window.location.host + '/music';
};
break;
case 'Interests':
return function () {
if (typeof window !== 'undefined')
window.location.href = '' + 'http://' + window.location.host + '/interests';
};
break;
default:
break;
}
}
for (var prop in menuOptionsAndIcons) {
if (menuOptionsAndIcons.hasOwnProperty(prop)) {
menu.push(<MenuItem leftIcon={menuOptionsAndIcons[prop]}
primaryText={ prop }
style={style.menuItem}
onTouchTap={_routeHandler.call(prop)}
key={prop}
rippleColor={"#FFFAE8"}
className="animated fadeInLeft"/>)
}
}
return menu
};
在事件处理程序中,你应该像这样使用
onClick={()=>this.yourfunction()}
不
onClick={this.yourfunction()}
- 为effect Composer创建GodRays效果过程
- 如何在JavaScript中将字符串转换为函数引用
- 如果 a 为 false,则 if(a) === if(false)
- 单击时将焦点更改为元素
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- 在哪里使用名为“;冻结”;
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 如何更改<svg>标记为<img>用js标记
- 货币代码为欧元-金额的格式不应包含小数
- 如何使用json将对象列表从java转换为javascript
- 为复选框javascript指定两个值
- 如何将json数据显示为html
- 我什至无法让 $().text 正常工作.改为输出大量代码
- JS “for (var key in arr)” >抛出我自己的 Array.prototypes,但不是固有的.为什
- 当我在 Amazon S3 中将安静设置为 true 以在 deleteObjects() 方法中分析安静模式时会发生什
- 我用AngularJS创建了一个单页房地产列表web应用程序,决定使用/$http服务提供数据,但没有成功.有人知道为什
- 在JavaScript中,console.log给出了for循环的每个值.但是var.text只显示最后一个值.这是为什
- 我有一个'Uncaught SyntaxError: Unexpected token函数'我不明白为什