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

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

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...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...