My use case is React, but this is a JavaScript question.
I'd like to extend the functionality of componentWillMount
by using a subclass. How can I accomplish this?
class Super {
componentWillMount() {
doStuff()
}
}
class Sub extends Super {
componentWillMount() {
super() // this doesn't work
doMoreStuff()
}
}
The syntax to use is:
super.componentWillMount()
From mdn:
The
super
keyword is used to call functions on an object's parent.The
super.prop
andsuper[expr]
expressions are valid in any method definition in both classes and object literals.Syntax
super([arguments]); // calls the parent constructor. super.functionOnParent([arguments]);
Demo:
class Super {
componentWillMount() {
console.log('parent')
}
}
class Sub extends Super {
componentWillMount() {
super.componentWillMount()
console.log('child')
}
}
new Sub().componentWillMount();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With