2016-04-22 60 views
0

好了,所以我想要做的是:ES6调用扩展组件功能

我延长反应,照片画廊成分,它有这个功能openLightbox,我想做些别的事情在我的组件当这个功能被触发时,我尝试了这样的事情:

import React, { Component } from 'react'; 
import { FormattedMessage } from 'react-intl'; 
import Gallery from 'react-photo-gallery'; 

export class GaleryComponent extends Gallery { 

    constructor(props) { 
     super(props); 
     this.openLightBox = this.openLightBox.bind(this); 

    } 
    openLightbox() { 
    console.log("LIGHTBOX OPENED"); 
    } 
} 

这样做的正确方法是什么?

+0

WTH是'this.openLightbox = super.openLightbox'应该做的吗?这覆盖了原有的覆盖方法。 – Bergi

回答

3
import React, { Component } from 'react'; 
import { FormattedMessage } from 'react-intl'; 
import Gallery from 'react-photo-gallery'; 

export class GaleryComponent extends Gallery { 

    constructor(props) { 
    // No need to set this.openLightbox, it will be available from the prototype chain 
    super(props); 
    } 

    // If you want to override openLightbox, you can use super and add your own code 
    openLightbox() { 
    super.openLightbox(); 
    console.log("LIGHTBOX OPENED"); 
    } 


    // Had you not overridden it, you would be accessing the super's openLightbox 
    componentDidMount() { 
     this.openLightbox(); 
    } 
} 
+0

嘿,尝试相同,仍然没有得到什么,这里是lib,https://github.com/neptunian/react-photo-gallery/blob/master/src/Gallery.js我真的不知道为什么它不是工作 –

+0

@MarkuzShultz我该怎么知道你是否没有解释它是如何工作的以及你使用的是什么代码?请参阅http://stackoverflow.com/help/how-to-ask和http://stackoverflow.com/help/mcve –

+0

Sry,我只是试图添加我的代码,当openLightbox函数从react-photo-画廊组件。当你的代码,我点击IMG,灯箱打开,但我没有得到console.log –