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

The google’s custom search at the top can be seen as an individual component, the navigation bar can be seen as an individual component, the sidebar is an individual component, the list of articles or post is also an individual component and finally we can merge all of these individual components to make a parent component which will be the final UI for the homepage.

Components in React basically returns a piece of JSX code which tells what should be rendered on the screen. In React we mainly have two types of components:

  1. 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. Below example shows a valid functional component in React:
    function Democomponent()
    {
        return <h1>Welcome Message!</h1>;
    }
    
  2. Class Components: The class components are little more complex than the functional components. The functional components are not aware about the other components in your program where as the class components can work with each other. We can pass data from one class component to other class component. We can use javascript ES6 classes to create class based components in React. Below example shows a valid class-based component in React:
    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 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 component only when we are sure that our component does not require to interact 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.

In this post, we will mainly write functional components to make things easier to understand. We will discuss class-based components in details later in the tutorial.

Rendering Components

In our previous post on rendering elements in React we have seen how elements intialized with DOM tags are rendered using ReactDOM.render() method. 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 first argument to the ReactDOM.render() method.
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 with html tags.

Below example renders a component named Welcome to the screen:

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react';
import ReactDOM from 'react-dom';
  
// This is a functional component
function Welcome()
{
        return <h1>Hello World!</h1>
}
   
ReactDOM.render(
    <Welcome />, 
    document.getElementById("root")
);

chevron_right


Output:

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

  1. We call the ReactDOM.render() with as the first parameter.
  2. React then calls the component Welcome, which returns <h1>Hello World!</h1>; as the result.
  3. 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”.

Reference:
https://reactjs.org/docs/components-and-props.html



My Personal Notes arrow_drop_up

Senior Technical Content Engineer | GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.