I have a component with a number of static functions defined for it(through the statics
property in the component definition). I need to access one of these static functions in the lifecycle method componentDidMount
. I tried the following
this
object has a statics
property, but that seems to be null
alwaysthis
object also has a _owner
, which in turn has a statics
property. Again, that is always null
Then I tried this.constructor.<static_function>
. This worked for me. I just wanted to know whether this is the right way to access static functions defined for a component or is there something else that I am unaware of.
Accessing your static methods and properties via this.constructor
is fine. You can also access them via ComponentClass.<static>
.
Why not define the functions in the outer scope, and just export them in the statics
property. Something like this:
var foo = function() { ... }
var bar = function() { ... }
var MyComponent = React.createClass({
statics: {
foo: foo,
bar: bar
}
});
Now the static functions are accessible anywhere in the scope of the component code.
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