Skip to content

Instantly share code, notes, and snippets.

@gil00pita
Last active January 25, 2019 19:50
Show Gist options
  • Save gil00pita/a0057f8e41c305f3ca52d7989a3f3eb1 to your computer and use it in GitHub Desktop.
Save gil00pita/a0057f8e41c305f3ca52d7989a3f3eb1 to your computer and use it in GitHub Desktop.
A class can extends the definition of another class, and a new object initialized from that class will have all the methods of both classes. The class that extends another class is usually called child class or sub class, and the class that is being
class ReactDeveloper extends Developer {
installReact(){
return 'installing React .. Done.';
}
}
var nathan = new ReactDeveloper('Nathan');
nathan.hello(); // Hello World! I am Nathan and I am a web developer
nathan.installReact(); // installing React .. Done.
class ReactDeveloper extends Developer {
installReact(){
return 'installing React .. Done.';
}
hello(){
return 'Hello World! I am ' + this.name + ' and I am a REACT developer';
}
}
var nathan = new ReactDeveloper('Nathan');
nathan.hello(); // Hello World! I am Nathan and I am a REACT developer
//Now that we understand ES6 class and inheritance, we can understand the React class defined in src/app.js. This is a React component, but it's actually just a normal ES6 class which inherits the definition of React Component class, which is imported from the React package.
//Everytime we need variables. Consider the following example:
import React, { Component } from 'react';
class App extends Component {
// class content
render(){
const greeting = 'Welcome to React';
return (
<h1>{greeting}</h1>
)
}
}
//This is what enables us to use the render() method, JSX, this.state, other methods. All of this definitions are inside the Component class. But as we will see later, class is not the only way to define React Component. If you don't need state and other lifecycle methods, you can use a function instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment