1

I understand sub classing with extends for example

class Car extends Vehicle {}
class Dog extends Animal {}

But with React, you may see

class HelloMessage extends React.Component {}

What does the dot between React and Component mean? How does it work in React and in vanilla Javascript?

1 Answer 1

4

Classes do not have to be standalone variable names - they may be properties of objects as well. So extends React.Component, absent any other context of what React is, just means that React is an object with has a Component property which is a class.

For an example of how to emulate this in vanilla JS:

const obj = {
  Foo: class Foo {
    doThing() {
      console.log('doing thing');
    }
  }
};

class MySubClass extends obj.Foo {
  subMethod() {
    console.log('submethod');
  }
}

const s = new MySubClass();
s.doThing();
s.subMethod();

React is doing the same sort of thing. It's just a way to organize data as properties of objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks for your explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.