0
export class NavMenu extends Component {
  static displayName = NavMenu.name;

  constructor (props) {
    super(props);

    this.toggleNavbar = this.toggleNavbar.bind(this);
    this.state = {
      collapsed: true
    };
  }

I tried searching for the word static in react but couldn't find my answer. What does the keyword static do in static displayName = NavMenu.name

momomo
  • 319
  • 1
  • 5
  • 15
  • 3
    This has nothing to do with React and everything to do with JavaScript. React is not a language. You're writing JavaScript. – Qix - MONICA WAS MISTREATED Jun 05 '21 at 03:24
  • 1
    Hope this answer can help clarify the meaning of Static: https://stackoverflow.com/questions/53796729/what-is-static-doing-in-react#:~:text=2%20Answers&text=Static%20means%20a%20property%20that,but%20not%20for%20it's%20instances.&text=Earlier%20in%20React%20we%20used,super(props)%20%7D%20%7D%20SomeClass – Aiman_Irfan Jun 05 '21 at 03:25
  • "static" is Javascript; not "React" per se. [TL;DR: Static properties are properties of a class, not of an instance of a class](https://medium.com/front-end-weekly/understanding-static-in-javascript-10782149993). See also [here](https://stackoverflow.com/a/57505687/421195), [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static) and/or [here](https://stackoverflow.com/a/1535687/421195) – paulsm4 Jun 05 '21 at 03:29

2 Answers2

0

The static keyword used in JavaScript classes mean that the property is that of the class itself (i.e. AClass.property) and not of the prototype or an instance (i.e. new AClass().property).

j1mbl3s
  • 994
  • 3
  • 16
0

In object orientation static attributes are attributes of the class and not the instances.

While a instance member would be accessed like this:

let instance = new ClassName();
console.log(instance.member);

A static member or "class member" would be accessed like this:

console.log(ClassName.member);

And therefore would be shared by all instances of that same class.

Static members are better than global variables or namespaced variables as their access can be limited by words like private, public or protected.

SparK
  • 5,181
  • 2
  • 23
  • 32