Skip to content

Instantly share code, notes, and snippets.

@machnicki
Created April 14, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save machnicki/b8c23a9c6a7432f29ed298812fd2d8f1 to your computer and use it in GitHub Desktop.
Save machnicki/b8c23a9c6a7432f29ed298812fd2d8f1 to your computer and use it in GitHub Desktop.
Event handlers in React - 3
import React, { Component } from 'react'
import { MyInput } from 'myInputs'
class MyComponent extends Component {
constructor(props) {
super(props)
this.handleKeyPress = this.handleKeyPress.bind(this)
}
handleKeyPress(item) {
return function(e) {
e.preventDefault()
if (e.nativeEvent.keyCode === 13) {
console.log(`This is enter on item, which is called ${item}!`)
}
}
}
renderItem(item) {
return (
<MyInput
onKeyPress={ this.handleKeyPress(item) }
/>
)
}
render() {
const items = ['Item1', 'Item2', 'Item3']
return (
<div>
{ items.map(this.renderItem) }
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment