10 React Class Based Components and Props
10 React Class Based Components and Props
10 React Class Based Components and Props
• React component (revision): A component is one of the core building blocks of React.
They serve the same purpose as JavaScript functions, but work independently and return
HTML via a render() function.
• React class components: The class components are a little more complex than the
functional components in React. We can use JavaScript ES6 classes to create class-based
components in React. When creating a React component, the component's name must start
with an upper-case letter. The component has to include the “extends” and
“React.Component” statement. This is to allow our component to inherit properties from
the default class called “Component” that contains the different React Lifecylce Methods.
• Understanding OOP, class and object before discussing class components in React:
Before explaining class components, let us revise our knowledge of objects and try to
understand what is meant by class, inheritance and the 2 different ways of creating objects
in JavaScript.
• Object Oriented Programming (OOP): It is a way we write applications/programs based
on classes and objects. In OOP everything is considered as an object. OOP is used to
structure applications/programs into simple and reusable blueprint code (called classes)
which are in turn used to create individual instances of objects. We can mention C++, Java,
Python and now JavaScript as OOP languages.
▪ Note: OOP focuses on the objects that developers want to manipulate than the
logic required to manipulate objects.
• Classes in OOP: Classes are blueprints used to create more specific, concrete objects that
share same attributes inherited from the blueprint class. Classes often represent broad
categories, like Person and define attributes like age and gender. Classes can also have
functions as their attributes called methods
• Objects in JavaScript: In JavaScript everything (including array) is an object except for
the primitive data types (Boolean, number and string, and undefined). Thus, if you master
objects in JavaScript, think as if you mastered a lot.
• Ways to initialize/create an object in JS (revision): There are different ways to create an
object in JavaScript
▪ Initializing an object literal: The object literal initializes the object with curly
brackets. Example: const objectLiteral = {};
• Note: Creating an object using object literal method is very limiting
because it creates only one object at a time.
let myPerson= {
name: “Abebe”,
Age: 13
};
▪ Initializing object constructor function with new object: The object constructor
initializes the object with the new keyword. Constructor function is the blueprint
that creates a new object and set values for any existing object properties.. Unlike
object literal, constructor function allows us to create as many objects (of similar
type) as we want. Example: let objectConstuctor = new Object();
• Note: To separate a regular function from an object constructor function,
we always have to use uppercase when giving it a name.
• Note: In a constructor function this does not have a value. It is a substitute
for the new object. The value of this will become the new object when a
new object is created.
• Note: Below, function Person() is an object constructor function that we
can use to create objects of the same type by calling the constructor
function with the new keyword.
this.name = name,
this.age = age
}
// Creating 2 objects using the same constructor above
• There are four pillars of OOP: Inheritance, encapsulation, abstraction and polymorphism
▪ Data Encapsulation: It is the process of containing/ wrapping up data in an object
and exposing only selected information. In short, it is a protective shield that
prevents the data from being accessed by the code outside this shield.
▪ Data Abstraction: Data Abstraction is the process of exposing only the required
characteristics of an object (such as high-level public methods accessible to all
classes in your app) and ignoring the irrelevant details.
▪ Data Polymorphism: Polymorphism is derived from two words; poly and
morphism which means many and variance of form. In programming,
polymorphism is defined as the ability of an object, varible or function to take on
different forms.
• Let us imagine that we will need to write a program that will calculate the
perimeter of shapes. Let us define shape as class and then define the
different shapes as sub-classes of the base class shapes. Therefore, we will
have a subclass circle, square and rectangle which will all have their
methods and different parameters. What we have done above is
polymorphism. Our shape class now has different forms and
characteristics like circle, square and rectangle. This means, any child
class object can take any form of a class in its parent hierarchy and also
take the reference from itself as well.
• In a programming language exhibiting polymorphism, class objects
belonging to the same hierarchical tree (inherited from a common parent
class) may have functions with the same name, but with different
behaviors.
▪ Data Inheritance: It is the process by which child classes inherit data and
behaviors from parent class. We will discuss this concept in detail below.
• Inheritance: In OOP languages, inheritance refers to a class's ability to access attributes
and methods/properties from another class. There are two types of inheritance:
▪ Class inheritance: Here, properties/methods from base class get copied into
derived class, in short, classes as blueprints for objects. PHP, Python, and Java are
class-based languages, which instead define classes as blueprints for objects.
▪ Prototypal inheritance: Every object in JavaScript has an internal property called
Prototype. To find the [[Prototype]] of this newly created object, we will use the
getPrototypeOf() method.
▪ Reusability: Reusability: Inheritance supports the concept of “reusability”, i.e.,
when we want to create a new class and there is already a class that includes some
of the code that we want, we can derive our new class from the existing class. By
doing this, we are reusing the fields and methods of the existing class.
• Prototypal inheritance in JavaScript: Before ES6, a JavaScript object could “inherit”
properties from another object via a prototype linkage. Prototype: A built-in property
every function/object in JavaScript has by default.
• Why use prototype in JavaScript: JavaScript uses prototype to add new
properties/methods an object constructor function. The new properties will be shared by
all objects that will later be created using this constructor. Look at the example below to
understand why we use prototype
▪ Example of adding grade prototype to constructor function:
this.name = name,
Student.prototype.grade = 5;
console.log(studentOne.grade); // 5
console.log(studentTwo.grade); // returns 5
▪ Example of adding a method prototype to constructor function:
this.name = name;
this.age = age;
Person.prototype.showAge = function () {
return this.age;
};
console.log(personOne.showAge()); // returns 33
▪ Example of linking 2 constructor functions: Assume you have a constructor
called Person that has 2 properties, name and age. This constructor has a third
property in a form of function (called showAge function) that is added to it as a
prototype. Assume there is another constructor called Student that inherits all
properties of our Person constructor. The Student constructor has also its own
property called grade. How can we use the showAge method from Person
constructor to get the age of any student object constructed from Student
constructor? Look at the following code
this.name = name;
this.age = age;
Person.prototype.showAge = function () {
return this.age;
};
this.grade = grade;
console.log(Student.prototype);
Student.prototype = Object.create(Person.prototype);
console.log(Student.prototype);// returns 33
• The Object.create() method: It creates a new object, using an existing
object (using the Person object in our case) as the prototype of the newly
created object (the Student object).
• JavaScript built-in constructors: JavaScript also has built-in constructors like new
Object() and new Array(). This means that we can create JavaScript string and number,
array as objects.
• We have seen above how property inheritance from one object to another used to happen
in JavaScript prior to ES6. This way of writing Object Oriented JavaScript works. But as
with other things, ES6 gives us a better way of doing it. Below, let us look at how
JavaScript uses class-based inheritance in ES6.
10.3 ES6 way of property inheritance in JS (concept of class)
• Just like other OOP languages, ES6 implements a class-based object-oriented approach. JS
developers wanted to use the class syntax to create object because most object-oriented
programming languages use class-based approach to handle inheritance.
• Classes in JavaScript: In ES6 we can create classes. Classes, just like ES5 constructor
functions, are templates used as a blueprint to create individual objects. Classes can contain
functions called methods available to instance objects of a class.
▪ Note: When we created components in our first React lectures, we advised you not
to use the keyword “class” in your JSX because we did not want React to confuse
the HTML attribute “class” with JavaScript “class”
• Class declaration/expression: One way to define a class is using a class declaration. To
declare a class, you use the class keyword followed by the name of the class
▪ Syntax for ES6 class declaration:
class Class_name {
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
getName() {
console.log(personOne.age); // prints 33
this.name = name;
▪ Class declarations: Classes must be defined before they can be used to construct
an object/class. In short, you cannot access the class before it is initialized.
Example: Code like the following will throw a ReferenceError
console.log(personOne.age);
class Person {
constructor(name, age) {
this.name = name;
• Concept of inheritance in JavaScript: We have seen in the previous sections that ES6
supports the concept of inheritance. To revise what discussed, inheritance is the ability of
a program to create new entities (example child classes) from an existing entity (example
parent classes).
• Using the “extends” Keyword to create a new class from an existing class: In
JavaScript, to create a class inheritance, use the extends keyword. In other words, we use
the “extends” keyword if we have a class already and we want to create a subclass/child
class that inherits all properties of the first class
o Parent/super class: The class that is extended to create newer classes is called the
parent class/super class.
o Child/sub class: The newly created classes are called the child/sub classes.
• Note: Child classes inherit all properties and methods except constructors
from the parent class.
o Syntax for class inheritance:
//parent class
class Person {
constructor(gender) {
this.gender = gender;
showGender() {
return this.gender;
class Person {
constructor(gender, rank) {
this.gender = gender;
this.rank = rank;
showGender() {
return this.gender;
showRank() {
return this.rank;
class ParentClass {
doPrint() {
doPrint() {
class CarBrand {
constructor(brand) {
this.brand = brand;
sayCarBrand() {
return this.brand;
constructor(brand, year) {
super(brand);
this.year = year;
class PrinterClassParent {
doPrint() {
doPrint() {
super.doPrint();
• Destructuring: We will need to understand the concept of array and object destructuring
as we will use destructuring tehnique when we pass data using props and states.
• Destructuring in JavaScript: It is a JavaScript feature that allows us to extract multiple
pieces of data from an array or object and assign them to their own variables. Destructuring
was introduced in ES6.
▪ Destructuring explained with real life example: To illustrate destructuring, it is
easier to understand the analogy of how we make a sandwich from items in our
fridge. If you want to make a sandwich, there is no need to take out everything you
have in the fridge. You only take out the items you would like to use on your
sandwich, such as cheese, vegetables and choice of your meat. Destructuring is
exactly the same. We may have an array or object that we are working with, but
we only need some of the items contained in these. Destructuring makes it easy to
extract only what is needed.
• Array Destructuring:
▪ Assigning array items before ES6: Please look how repetitive it is to assign each
item of the array.
• Example:
• Note 3: When destructuring arrays, if you want only few of the array
elements, you cans simply leave out the one you want to leave but keep a
comma as a placeholder for that element. Example:
function calculate(a, b) {
const ad = a + b;
const subt = a - b;
const mult = a * b;
const div = a / b;
function calculate(a, b) {
const ad = a + b;
const subt = a - b;
const mult = a * b;
const div = a / b;
function getProfile() {
balcha = 20;
console.log(alem); // 10
console.log(balcha); // 20
alem = balcha;
balcha = tempVar;
console.log(alem); // 20
console.log(balcha); // 10
• Swapping variables values using destrcuturing: Knowing how to
destructure an array, it's easy to use it for swapping variables. This
approach let us write cleaner and shorter code.
balcha = 20;
console.log(alem); // 10
console.log(balcha); // 20
console.log(alem); // 20
console.log(balcha); // 10
console.log(array[2]); // prints 2
array[1] = array[2];
array[2] = tempVar;
console.log(array[1]); // prints 2
console.log(array[2]); // prints 2
console.log(array[1]); // prints 2
const students = {
name: "Haile",
age: 22,
};
console.log(studentAge); // prints 22
theName: "Haile",
theAge: 22,
};
console.log(theAge); // prints 22
• Note: If the destructured object doesn't have the property specified in the
destructuring assignment, then the variable is assigned with undefined.
Example:
let students = {
theName: "Haile",
theAge: 22,
};
let students = {
theName: "Haile",
theAge: 22,
};
console.log(theAge); // prints 22
let students = {
theName: "Haile",
theAge: 22,
theGenderAndHeight: {
theGender: "Male",
theHeight: 133,
},
};
console.log(theAge); // prints 22
let students = {
theName: "Haile",
theAge: 22,
theGender: "Male",
theMood: "Happy",
};
console.log(theRemainingProperties);
function myFunc(myparam) {
• React class component: After ES6, to create a class component in React you only need to
define the component as a plain JavaScript class that we have covered above.
• React class component before and after ES6: When React was initially released in 2013
and until ES6 was released in 2015. JavaScript did not have classes. Because JavaScript
didn't have a built-in class system then, React developers used React’s 'create-react-class'
module to create class components. With ES6, React now allows you to implement
component classes that use ES6 JavaScript classes. The result is the same, both ways can
create a component class. But the syntax is different.
▪ Syntax of React before ES6: We do not need to use this syntax now
render: function() {
return <h1>Hello</h1>;
});
▪ Syntax of React after ES6: Creating a class component is pretty simple if you
follow the following steps:
• Import the React and Component from React
• Define a class that extends Component and has a render function.
• Export your component to make it available for other compoents
• This is the syntax we now use to create class components.
render() {
return <h1>Hello</h1>;
• Note: The above syntax is the one that we will use to create class
components
• Explaining each term/keyword in class component
▪ import React, { Component } from 'react:
• Import React: If you have created a new react app using creat-ereact-app
recently, you will notice that your React app works fine without even
importing React from “react”. We needed to import the “react” library
because it is used to convert our JSX into vanilla JavaScript using the
library’s createElement() method for the browser. However, after the
release of React 17, our JSX is converted to vanilla JavaScript
automatically without using React library’s createElement() method.
• Component: React has different built-in methods (called React lifecycle
methods). These built-in methods are found under a React class called
Component. You will need to import the Component class if you want to
use this class in your component
▪ class YourComponent extends Component”: If you want your new component
to inherit properties of the Component class, in addition to importing this class,
you have to make sure that your component to extend the Component class.
▪ render(): The render () method in class-based components returns your JSX (then
converted to HTML) that will be rendered later in our index.html
▪ export default YourComponent: This is basically making your new component
available for other components to use it
• Class vs functional components in React: In addition to their syntax, there are major
differences between class-based and functional components. Those differences will be
clearer once we get to learn about props and states in later classes. For now, just focus on
learning how to convert your functional components to class-based components
• Steps to converting functional components into class-based components:
▪ Step 1: Replace the function key word with class
▪ Step 2: Inherit properties of Component class by extending
▪ Step 3: define the render method in your subclass then in your render() method,
return the JSX you want to return
▪ Step 4: If you are passing props, make sure to use the key word "this" to
specifically identify your current class
10.7 React props
• Passing data between React components: Data sometimes needs to be able to move from
children to parent, from parent to children, or between sibling components. Let us assume
you want to change the data in a component wihtout actually hard coding the change in
that component. Example: You have a component called GreetPeople that renders the
message Hello followed by name (name that changes from one name to another). One way
to have the greet name to change is to create different components that render the Hello
message with different names. Another way to have the name change dynamically is using
props
• Props: Props is just a shorter way of saying properties. They are properties that hold
information about a component. Props are variables that get passed to the component from
parent component (like a parameter is passed to a function). We use props in React to pass
data from one component to another (from a parent component to a child component(s))
▪ Props are objects of React and contain the number of key-value pairs.
▪ Props are objects with immutable properties, meaning, we cannot change their
value throughout the component. This is because changing value of props won’t
make sense, it will be like changing the argument of an adder function to fixed
parameter.
▪ Props can be available in both classes based and function-based components
without any issue.
• Using props to pass data from parent to child components: This is the easiest direction
in React to transfer data. If you have access to data in your parent component that you need
your child component to have access to, you can pass data from parent as a prop to the
child by initiating the prop within the parent. Let us start by defining what props are and
how to use props in our components
▪ Note: React’s data flow between components is uni-directional (from parent to
child only).
▪ Note: You can use props in both functional and class components. You can even
use functional component for your parent component and use a class component
for your child component while using props.
• Why use props:
▪ We use props in React to pass data from one/ parent component to a child
▪ Props allow us reuse components dynamically. It is just like creating a function
that takes an argument. Different components will have their own specific
properties. Meaning the data will not be static as the component’s data can be
modified to fit what we want.
• Steps to use props: To understand the steps below, please look at the class demo on props
(HeaderLinks.js, Fourth.js components on the Apple website)
▪ Step 1: Create two components; a parent and a child component
▪ Step 2: Import the child component in the parent component and return it.
▪ Step 3: Import and return the parent component in your app.js
▪ Step 4: Initiate/declare the props in your parent component
▪ Step 5: Send props into a parent component,
• By returning your props as HTML attributes:
o Example: return <ChildNavigation LinkURL="/Mac" />; or
• By returning your props as string:
render() {
render() {
function FufiChild(props) {
return (
<h1>Hello {props.Name}</h1>
);
//Child component
function FufiChild(props) {
return (
<h1>
</h1>
);
// Parent component
render() {
Gender="male" />;
// Parent component
render() {
let allProps = {
Name: "Abebe",
Gender: "male",
};
//Parent component
render() {
return (
<div >
<ChildDestructuringProps Name="Abebe"
Age="is 55" />
</div>
);
}
//Child component
function ChildDestructuringProps(props) {
return (
<div>
{Name} {Age}
</div>
);
o Destructuring props in class components: When we access the props using the
“this” keyword, we have to use this/ this.props throughout the program, but by the
use of destructuring, we can discard this/ this.props by assigning them in new
variables. This is very difficult to monitor props in complex applications.
▪ So, how do we destructure props in class components? We destructure
props in the render() function under the child component by assigning all of
our props on a new variable. Please note that unlike functional components,
in class components we do not acheive destructuring when the props are
passed in in the function’s argument. We achieve the destructuring of props
in class components wherever the variables are called.
▪ Notice below that destructuring our props gives us access to both the
“Name” and the “Age” properties, which we need for different children
components. Example:
//Parent component
render() {
return (
<div >
<ChildDestructuringProps Name="Abebe"
Age="is 55" />
</div>
);
render() {
return (
<h1>{Name} {Age}</h1>
);