2017-08-21 54 views
0

我有一个类组件,其中有一个onClick事件处理程序,它引用内部参考input。但是,事件处理程序input为空。我在构造函数中将事件处理函数绑定到thisReact - 无法访问有状态组件事件处理程序中的引用

import React, { Component} from 'react'; 

class MyComponent extends Component { 
    constructor(props) { 
    super(props); 
    this.onClick = this.onClick.bind(this); 
    } 

    onClick(e) { 
    // The following throws "Cannot read property 'click' of undefined" 
    this.input.click(); 
    } 

    render() { 
    return (
     <div className="container" onClick={this.onClick}> 
     <input type="text" ref={input => this.input = input} /> 
     </div> 
    ); 
    } 
} 

为什么this.input未定义在我的事件处理程序?

编辑 显然代码运行良好,只是不在我的环境中。我正在使用webpackbabel与环境和反应预置w /热重新加载。我的目标是电子

完整的错误堆栈:

my-component.jsx:12 Uncaught TypeError: Cannot read property 'click' of undefined 
    at MyComponent.onClick (http://localhost:8080/renderer.js:19224:15) 
    at Object.ReactErrorUtils.invokeGuardedCallback (webpack:///./node_modules/react-dom/lib/ReactErrorUtils.js?:69:16) 
    at executeDispatch (webpack:///./node_modules/react-dom/lib/EventPluginUtils.js?:85:21) 
    at Object.executeDispatchesInOrder (webpack:///./node_modules/react-dom/lib/EventPluginUtils.js?:108:5) 
    at executeDispatchesAndRelease (webpack:///./node_modules/react-dom/lib/EventPluginHub.js?:43:22) 
    at executeDispatchesAndReleaseTopLevel (webpack:///./node_modules/react-dom/lib/EventPluginHub.js?:54:10) 
    at Array.forEach (native) 
    at forEachAccumulated (webpack:///./node_modules/react-dom/lib/forEachAccumulated.js?:24:9) 
    at Object.processEventQueue (webpack:///./node_modules/react-dom/lib/EventPluginHub.js?:254:7) 
    at runEventQueueInBatch (webpack:///./node_modules/react-dom/lib/ReactEventEmitterMixin.js?:17:18) 

编辑

想通了看到我的回答如下。

+0

此代码没有任何问题。它会工作。你需要写更多的细节。 –

+0

我跑你的代码,它的工作... –

+0

奇怪的是,它适用于我,当我在线编辑器中运行它,但不是在我的webpack环境中。 – Bob

回答

0

我认为你正试图获得输入值。如果是的话这里是代码。没有this.input.click();方法定义

import React from 'react'; 

class MyComponent extends React.Component { 
    constructor(props) { 
    super(props); 
    this.onClick = this.onClick.bind(this); 
    } 

    onClick(e) { 
    // The following throws "Cannot read property 'click' of undefined" 
    console.log(this.input.value); 
    } 

    render() { 
    return (
     <div className="container"> 
     <input type="text" ref={input => this.input = input} onChange={this.onClick}/> 
     </div> 
    ); 
    } 
} 

export default MyComponent; 
+0

'this.input'在我的环境中由于某种原因未定义。 – Bob

+0

你可以试试'this.textInput'而不是'this.input' –

+0

试过'这个。textInput',我仍然得到相同的错误。 – Bob

0

您是否尝试重写你的onClick回调为箭头的功能?这也将让你的代码变得更小:

import React, { Component } from 'react'; 

class MyComponent extends Component { 
    this.input = null; 

    onClick =() => { 
    this.input && this.input.click(); 
    } 

    render() { 
    return (
     <div className="container" onClick={this.onClick}> 
     <input type="text" ref={input => this.input = input} /> 
     </div> 
    ); 
    } 
} 

即使它不应该的问题,我也检查this.input设置 - 但你可以忽略的那部分(通常情况下)。

+0

尝试使用transform-class-properties插件并获得相同的结果。 – Bob

+0

如果将'onClick = {()=> console.log(this.onClick)}'括入箭头函数并注销,会发生什么? – lumio

+0

哦,对不起,我编辑了你的代码。 –

相关问题