2017-08-21 52 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

哦,對不起,我編輯了你的代碼。 –

相關問題