Skip to main content

React JS Components

React JS Components

 Hello and welcome to trycoder.com where you learn how to code so easily. In this tutorial, we are going to talk about React JS component is all about and learn how to code it out. If you are new in learning React JS, I would advice you keep it up because it is very demanding in the Job market. A shout out to all React JS developers!!!!

I recommend you subscribe to our newsletter, YouTube channel and frequently visit our blog to get more awesome stuff in your programming language or framework.

Now let’s get started…

What are components?

In React JS, a component can be likened to a function, but returns HTML elements.

Just like functions, components can be re-used.

Components are pieces which form the UI of an application. For example, you might have a header component, a footer component and sidebar component.

Similarly, as regular vanilla JavaScript functions accepts parameters or arguments, React JS components equally accept properties or props which we will cover in our next tutorial.

How do we create components?

There are actually 2 categories of components;

  • Class components and
  • Functional components

Below is an example of a class component;

class Greetings extends React.Component {
render() {
return

Hello, world

; } }

The class component above begins with the keyword class and has a render function which returns an HTML element.

Below is a functional component:

function Greetings (){
   return 

Hello, world

; }

The Greetings components above do the same thing. But you might be asking whether it is better to use class or functional components?

Well, each have their advantages and limitations.

Rule : Start all component names with Capital letters, be it functional or class component.

How do we render these components?

The render function accepts two arguments which are the code to be rendered which is usually in the components and the container (root node) which will hold the html elements or code.

So the component is rendered by simply adding the name of the component like a tag as the first argument of the render () before the root node.

In your create-react-app boiler-plate, edit the index.js file as follows;

import React from 'react';
import ReactDOM from 'react-dom';
class Greetings extends React.Component {
    render() {
      return 

Hello, world

; } } ReactDOM.render(, document.getElementById('root'));

Looking at the code above, you see how I rendered the greetings component;

<Greetings />

OUTPUT

React JS Components
There you go, it worked successfully!

How About Components In External  File?

To create a component in an external file, you should create a file with the .js extension and then import any modules or features you might want to import, type your component and them export.

After doing so, import the external js file component into your main app and then use. Below is a typical example.

Lets create a file called Test.js and type our code (component);

Test.js

Remember, the function name or component name should always begin with a capital letter. Notice also how we exported the component with same name as function name.

Now let’s import, render and see the output.

index.js

OUTPUT

React JS Components

Thanks.

See you in the next tutorial.

Comments

Popular posts from this blog

JavaScript Functions

  Hi there, and welcome to another exciting lesson on JavaScript. If this is your first time here, please do well to check out our previous lessons. In the previous lesson, we were discussing JavaScript math methods. It was very intuitive as we got to learn how to use the various math methods. To proceed, we shall be looking at JavaScript functions.  A function in JavaScript like in every other programming language could be defined as a block of code that is written to perform a particular task, and this function is usually invoked or called before it is being implemented. We have been talking about methods throughout the previous lessons right. Do you know that those methods are actually functions? Yes they are functions. You can now have an overview of the importance of functions in every programming language. How do we create a function in JS? To create a function, you follow the format function functionName(Argument) {//Block of code }. Some functions do have a return val...

JavaScript Math Methods

Hello everyone, and welcome to another exciting JavaScript lesson. In the last lessons you have been seeing other methods being used in JS (for example, the string methods). We shall go further into exploring other methods. This time, it’s going to be math methods. Do not move an inch because it’s going to be a very exciting. Before we look at what a math method is, let’s have an overview of math objects. A math object in JavaScript is a static built-in object that includes properties and methods used in performing mathematical tasks. Talking about math properties, they have the syntax Math.property . Some examples are Math.E that returns Euler’s number, Math.PI that returns PI, Math.LN2 that returns the natural logarithm of 2, and many others. The various JavaScript methods contained in the math object, thus, make mathematical operations easier and reduce effort as well as time in math-oriented programming. Some JS methods include abs( ), ceil( ), cos( ), sqrt( ), pow( ), log( ) ...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...