Quantcast
Channel: React utility function to bind this keyword - Stack Overflow
Viewing all articles
Browse latest Browse all 3

React utility function to bind this keyword

$
0
0

I have a utility function, but this is undefined even I bind it in my react component:

/util/index:

export const handleKeyPress = e =>{
  console.log(this)
}

/component/input/:

import {handleKeyPress}  from "../util/index"
class Input extends Component {
  constructor(props) {
    super(props)
    this.state = {
        isOpen: false
    }
  }
  this.handleKeyPress = handleKeyPress.bind(this)

  render(){
   return <input onKeyPress = {this.handleKeyPress} />
 }
}

So on my input keypress, this is undefined in the function even I bind it inside my constructor. What can I do?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images