connect() doing something to static methods in parent class?
See original GitHub issueNo idea why this is happening. Any clues?
class Bar {
static renderNavigationBar(props) {
return <CustomView />;
}
}
class Foo1 extends Bar {}
The function of the static method is probably irrelevant, but it tells rn to use that custom view for the navigation bar: https://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/Navigator/Navigator.js#L1120
This works as expected:
export default Foo1;
And without inheritance, this works as expected:
class Foo2 {
static renderNavigationBar(props) {
return <CustomView />;
}
}
export default Foo2;
But with inheritance, this doesn’t work – it’s as if there was no static method on parent.
export default connect(...)(Foo1)
Is this a redux issue?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Java: Use Static methods of Parent Class in Child Class
To call static method of your parent class, you have to write class name first: BaseComponentType.findById() ;. If you want to get rid...
Read more >Why static methods of parent class gets hidden in child class ...
Overriding Static Methods The method in the superclass will be hidden by the one that is in the subclass. This mechanism is known...
Read more >static - JavaScript - MDN Web Docs - Mozilla
The static keyword defines a static method or property for a class, or a static initialization block (see the link for more information ......
Read more >Traits - Manual - PHP
Unlike inheritance; if a trait has static properties, each class using that trait has independent instances of those properties. Example using parent class:...
Read more >How to call child static method from parent static method
Other possibility would be to make Greet abstract and implement it in each child class using child class name instead of the obj,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is generally a good rule in React. You shouldn’t use inheritance with it.
I would not recommend using inheritance for this. What if next time, you want to share some other code between other set of your classes? But now they inherit from a common parent, so you’d have to use some hack or a mixin system to copy those methods. At the end of the day there is no reason why it is shorter than just a function:
In React, inheritance is discouraged. Just don’t use it, you don’t need it. You can achieve exactly the same reusability with functions.
TL;DR: Don’t extend anything besides default
React.Component
in your components if you plan on connecting them to Redux.I ran across this issue this morning (or something similar). I’m using
react-native-router-flux
and wanted to add a custom navbar to a bunch of scenes. I decided to abstract out thestatic renderNavigationBar(props)
to a separate class like this:and use that in all of my classes like this:
This works great until you attempt to connect the components to redux via
connect(...)
because it seems the connect method is returning a new Component or as you said:For anyone who cares how I solved this, I just included a standard
in each of my components. This serves to explicitly signify which components have a navigation bar as well as allow me to customize it a bit more than just extending a class. If this is not what this original ticket is about, I’m sorry for wasting so much of your time.