我有一个使用 material-ui 的项目列表。我想在单击项目时调用 _handleTouchTap() 并将 ListItem 的键传递给处理程序。
添加
onTouchTap={this._handleTouchTap}
to 不起作用,因为“this”似乎是错误的范围
var React = require('react');
var Mui = require('material-ui');
var ThemeManager = new Mui.Styles.ThemeManager();
ThemeManager.setTheme(ThemeManager.types.LIGHT);
var injectTapEventPlugin = require('react-tap-event-plugin');
injectTapEventPlugin();
var List = Mui.List
var ListItem = Mui.ListItem
var Main = React.createClass({
childContextTypes: {
muiTheme: React.PropTypes.object
},
getChildContext: function () {
return {
muiTheme: ThemeManager.getCurrentTheme()
}
},
render: function() {
var items = [
{id: 1, title: 'Item 1'},
{id: 2, title: 'Item 2'},
{id: 3, title: 'Item 3'}
]
return (
<List>
{items.map(function(item){
return <ListItem key={item.id} primaryText={item.title} />
})}
</List>
)
},
_handleTouchTap: function() {
// the key of the item should be passed though here
}
});
React.render(<Main />, document.body);