2016-03-02 46 views
0

在我的組件定義的反應,我追加的事件監聽像這樣:上的onClick調用功能的陣營

/* events.js */ 
export function mouseyDown(event) { 
    console.log(event.target); 
} 

/* main.js */ 
import mouseyDown from '../controllers/events.js'; 
const Topbar = React.createClass ({ 
    callMouseyDown : function(event) { 
     console.log(event.target); 
     mouseyDown(); // actually need to pass event to this method too 
    }, 
    render : function() { 
     return (
      <div className="tbIcon" data-action="shareSocial" onClick={ this.callMouseyDown}>G</div> 
    )} 
}); 

然而,反應是給我一個_EventController2.default is not a function錯誤。 我在做什麼錯?

回答

1

Mousedown不是已創建對象的屬性,而是您已導入的函數。因此,你需要省略this關鍵字:

onClick={mouseyDown} 

而且你需要你的函數導出爲默認:

export default function mouseyDown(event) { 
    console.log(event.target); 
} 
// ... 
import mouseyDown from '../controllers/events.js'; 

或使用名爲進口:

export function mouseyDown(event) { 
    console.log(event.target); 
} 
// ... 
import {mouseyDown} from '../controllers/events.js'; 
+0

謝謝,這似乎沒有工作。我沒有錯,什麼都沒有。爲了清楚起見,我重新命名了onClick函數。 – Kayote

+0

@Kayote看到我的更新 – madox2

+0

你是最高階的天才。謝謝。 – Kayote