Open In App
Related Articles

ReactJS Components

Improve Article
Improve
Save Article
Save
Like Article
Like

Components in React are the code blocks representing the UI elements. These blocks are reusable and independent like functions in JavaScript and are responsible for representing the elements on the Web Page.

Prerequisites:

What are React JS Components ?

A Component is one of the core building blocks of React. In other words, we can say that every application you will develop in React will be made up of pieces called components. Components make the task of building UIs much easier. You can see a UI broken down into multiple individual pieces called components and work on them independently and merge them all in a parent component which will be your final UI. 

You can see in the below image we have broken down the UI of GeeksforGeeks’s homepage into individual components. 

gfgdrawio-(1)

Components in React basically return a piece of JSX code that tells what should be rendered on the screen.

Types of Components in React JS :

In React, we mainly have two types of components: 

Functional Components:

Functional components are simply javascript functions. We can create a functional component in React by writing a javascript function. These functions may or may not receive data as parameters, we will discuss this later in the tutorial. The below example shows a valid functional component in React:

Syntax for Funtional Components:

function demoComponent() {
return (<h1>
Welcome Message!
</h1>);
}


Class Components:

The class components are a little more complex than the functional components. The functional components are not aware of the other components in your program whereas the class components can work with each other. We can pass data from one class component to another class component. We can use JavaScript ES6 classes to create class-based components in React. The below example shows a valid class-based component in React: 

Syntax for Class Components:

class Democomponent extends React.Component {
render() {
return <h1>Welcome Message!</h1>;
}
}

The components we created in the above two examples are equivalent, and we also have stated the basic difference between a functional component and a class component. We will learn about more properties of class-based components in further tutorials.

For now, keep in mind that we will use functional components only when we are sure that our component does not require interacting or work with any other component. That is, these components do not require data from other components however we can compose multiple functional components under a single functional component.

We can also use class-based components for this purpose but it is not recommended as using class-based components without need will make your application in-efficient. 

Rendering Components in ReactJS

React is also capable of rendering user-defined components. To render a component in React we can initialize an element with a user-defined component and pass this element as the first parameter to ReactDOM.render() or directly pass the component as the first argument to the ReactDOM.render() method. 

The below syntax shows how to initialize a component to an element: 

const elementName = <ComponentName />;

In the above syntax, the ComponentName is the name of the user-defined component. 
Note: The name of a component should always start with a capital letter. This is done to differentiate a component tag from HTML tags.

Example: This example renders a component named Welcome to the Screen.

javascript




// Filename - src/index.js:
 
import React from "react";
import ReactDOM from "react-dom";
 
// This is a functional component
const Welcome = () => {
    return <h1>Hello World!</h1>;
};
 
ReactDOM.render(
    <Welcome />,
    document.getElementById("root")
);


Output: This output will be visible on the http://localhost:3000/ on the browser window.

gfg

Explaination:

Let us see step-wise what is happening in the above example: 

  • We call the ReactDOM.render() as the first parameter.
  • React then calls the component Welcome, which returns <h1>Hello World!</h1>; as the result.
  • Then the ReactDOM efficiently updates the DOM to match with the returned element and renders that element to the DOM element with id as “root”.

For more information on component open component set 2


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 21 Nov, 2023
Like Article
Save Article
Similar Reads
Related Tutorials