Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React实用程序函数绑定此关键字_Reactjs_This_Es6 Modules - Fatal编程技术网

Reactjs React实用程序函数绑定此关键字

Reactjs React实用程序函数绑定此关键字,reactjs,this,es6-modules,Reactjs,This,Es6 Modules,我有一个实用函数,但是这个是未定义的,即使我将它绑定到我的react组件中: /util/index: export const handleKeyPress = e =>{ console.log(this) } /component/input/: import {handleKeyPress} from "../util/index" class Input extends Component { constructor(props) { super(props)

我有一个实用函数,但是
这个
是未定义的,即使我将它绑定到我的react组件中:

/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} />
 }
}
从“./util/index”导入{handleKeyPress}
类输入扩展组件{
建造师(道具){
超级(道具)
此.state={
伊索彭:错
}
}
this.handleKeyPress=handleKeyPress.bind(this)
render(){
返回
}
}

因此,在我的输入键上,
这个
在函数中是未定义的,即使我将它绑定到构造函数中。我能做什么?

如果不想在构造函数中编写绑定,可以使用arrow函数编写事件

render()
return <input onKeyPress = {() => 
this.handleOnKeyPress() }/>
 } 
render()
返回
this.handleOnKeyPress()}/>
} 

之后,您可以删除组件的绑定代码行。

如果不想在构造函数中编写绑定,可以使用arrow函数编写事件

render()
return <input onKeyPress = {() => 
this.handleOnKeyPress() }/>
 } 
    import { handleKeyPress } from "../util/index"
    class Input extends Component {
      constructor(props) {
        super(props)
        this.state = {
          isOpen: false
        }
      }

      render() {
        return <input onKeyPress={(e) => handleKeyPress(e)} />
      }
    }
render()
返回
this.handleOnKeyPress()}/>
} 
之后,您可以删除组件的绑定代码行。

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

      render() {
        return <input onKeyPress={(e) => handleKeyPress(e)} />
      }
    }
类输入扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 伊索彭:错 } } render(){ 返回手柄按键(e)}/> } }
从调用函数中删除它,并从“./util/index”中删除绑定函数行

import{handleKeyPress}
类输入扩展组件{
建造师(道具){
超级(道具)
此.state={
伊索彭:错
}
}
render(){
返回手柄按键(e)}/>
}
}

从调用函数中删除它并删除绑定函数行

想知道为什么在util函数中需要
这个
,这样我就可以在其他类似的组件中重用它export const handleKeyPress=e=>{console.log(this)}///by
this
你是说
输入
类吗?是的,假设我想用util函数更改状态
isOpen
,但是util函数无法识别
this.state
。想知道为什么在util函数中需要
this
,这样我就可以在其他类似组件中重用它了导入const handleKeyPress=e=>{console.log(this)}///这里所说的
this
是指
输入
类吗?是的,假设我想用util函数更改状态
isOpen
,但是util函数无法识别
这个状态。