Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use in imported js class ReactJS

Introduction

JavaScript (JS) has grown exponentially over the years, evolving into an essential language for web development. One of the critical advancements in this language is the introduction of classes, which are a feature of Object-Oriented Programming (OOP). In simple terms, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). ReactJS, on the other hand, is a JavaScript library for building user interfaces or UI components. It's maintained by Facebook and a community of individual developers and companies.

In this article, we will focus on how you can use an imported JavaScript class in ReactJS. Don't worry if these terms are a bit confusing, we'll break down everything step by step and use simple analogies to make the concepts clear.

Understanding JavaScript Classes

Think of a JavaScript class as a cookie cutter. This cutter (class) has a specific shape (properties) and can be used to make multiple cookies (objects) of the same shape. The 'Class' is just a blueprint, but it doesn't have any substance on its own until it's used to create an 'Object'.

Here's a simple example of a JavaScript class:

class Car {
  constructor(brand) {
    this.carname = brand;
  }
}

let myCar = new Car("Toyota");

In the above example, Car is a class, and myCar is an object of the Car class. 'Toyota' is a property of myCar.

Importing a JavaScript Class in ReactJS

Now that we understand what a JavaScript class is, let's see how we can use it in a ReactJS application. Just like you might import a book into your library, you can import a class into your ReactJS component.

Say you have a JavaScript file Car.js with the following Car class:

class Car {
  constructor(brand) {
    this.carname = brand;
  }

  present() {
    return 'I have a ' + this.carname;
  }
}

export default Car;

In this file, we have a Car class with a constructor that accepts a brand argument and a present method. The export default Car; line means that this class can be imported in another file.

To use this class in a ReactJS component, we first need to import it. This is done using the import keyword. Let's say we have a ReactJS component Garage.js:

import React from 'react';
import Car from './Car';

class Garage extends React.Component {
  render() {
    const myCar = new Car("Toyota");
    return <h2>{myCar.present()}</h2>;
  }
}

export default Garage;

In the Garage component, we first import the Car class from Car.js. Then, we create a myCar object from the Car class, giving it a brand name of 'Toyota'. Finally, we display the result of myCar.present() in an <h2> element.

Breaking Down the Process

Let's break down the process to make it more understandable:

Create a class in a JavaScript file: This is like writing down a recipe. The class outlines the properties and methods of the objects you'll be creating.

Export the class: This is like photocopying the recipe. The 'export' keyword allows you to use the class in other files.

Import the class in a ReactJS component: This is like bringing the photocopied recipe into your kitchen. The 'import' keyword brings the class into your ReactJS component.

Create an object of the class: This is like following the recipe to make a dish. The new keyword creates an object from the class.

Use the object: This is like serving the dish you've made. You can now use the object and its methods in your ReactJS component.

Conclusion

That's it! You now know how to import and use a JavaScript class in a ReactJS component. Just remember that a class is a blueprint for creating objects, and you can import this blueprint into your ReactJS component to create and use objects.

Remember, learning programming is like learning a new language. It's normal to feel overwhelmed at first, but with practice and patience, you'll get the hang of it. Keep coding and exploring!