Using this keyword access component properties and methods


The this keyword refers to the component instance and required to specify the properties and method defined by the component. Here, dimension refers to the state property created in constructor.

Example

import React, { Component } from 'react';
 
const length = 20, breadth = 10;
 
function shape() {
    return length == breadth ? "Sqaure" : "Rectangle";
}
 
function area() {
    return length * breadth;
}
 
class App extends Component {
  render = () => 
  <span class="d-block p-2 bg-dark text-white">
    Area of { shape() }: { area() }
  </span>
}
 
export default App;
Most Helpful This Week