Cannot Override A Method In React
I have two components: var A = React.createClass( { doSomething: function() { return 'I am A' }, render() { return(
{this.doSomething()}
Solution 1:
In the code above doSomething
is instance method on A and prototype method in B.
It's roughly same as
classAextendsReact.Component {
doSomething = () => {
console.log("I am A");
}
}
classBextendsA {
doSomething() { ... }
}
And doSomething
from A beats doSomething
from B in prototype chain. It's conventional to stick to prototype properties for methods in ES6 classes everywhere to avoid problems like this one. If this is not possible, doSomething
should be made an instance method in child class, too:
classBextendsA {
doSomething = () => { ... }
}
Post a Comment for "Cannot Override A Method In React"