[英]es6 call extended component function
Ok so what I'm trying to do is : 好的,我想做的是:
I'm extending react-photo-gallery component and it has this openLightbox function, and I want to do something else in my component when that function is triggered, I tried something like this: 我正在扩展react-photo-gallery组件,它具有这个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");
}
}
What's the proper way to do this? 正确的方法是什么?
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.