SlideShare a Scribd company logo
What are the
components in React?
What are the components in React?
• We can say that every application you will develop in React will be
made up of pieces called components. So, in this article, we will see
what are the components in React.
• A Component is one of the core building blocks of React. It makes the
task of building UIs much easier. You can see a UI broken down into
multiple individual pieces called components. Also, you can work on
them independently and merge them all in a parent component
which will be your final UI.
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 posts 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 return
a piece of JSX code that tells what should be rendered on the screen. 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
Below example shows a valid functional component in React:
const 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 other
class components. 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 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 our previous post on rendering elements in React we have seen
how elements initialized 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 the 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. The name of a component should always start
with a capital letter. This is done to differentiate a component tag with
HTML tags. The below example renders a component named Welcome
to the screen. Open your index.js file from your project directory, and
make the given below changes:
• 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")
• );
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”.
Conclusion:
• So, in this article, we have been through what are the components in
React. Also, feel free to comment with your suggestions and feedback
on the post. Moreover, at BOSC Tech Labs, we have a team of highly
experienced React JS developers. They can assist you in developing
your customized web app. So contact us to hire experienced ReactJS
developers.

More Related Content

PDF
Get rid of controllers in angular 1.5.x start using component directives
PPTX
React js
PPTX
PDF
Web components are the future of the web - Take advantage of new web technolo...
PDF
[React Native Tutorial] Lecture 6: Component, Props, and Network
PDF
Redux and context api with react native app introduction, use cases, implemen...
PPTX
PPTX
Introduction to angular 2
Get rid of controllers in angular 1.5.x start using component directives
React js
Web components are the future of the web - Take advantage of new web technolo...
[React Native Tutorial] Lecture 6: Component, Props, and Network
Redux and context api with react native app introduction, use cases, implemen...
Introduction to angular 2

What's hot (20)

PPTX
Angular 4 - quick view
PDF
PDF
Fundamental concepts of react js
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
PDF
Angular 4 for Java Developers
PDF
Angular 2 - An Introduction
PDF
Introduction To Angular 4 - J2I
PDF
AngularJS 2.0: A natural evolvement or a new beginning - Boyan Mihaylov - Cod...
PDF
Introduction to React Hooks
PPTX
AngularJS 2.0
PPTX
Dive into Angular, part 4: Angular 2.0
PPTX
Reactjs
PPTX
Angular 9
PPTX
Introduction to angular with a simple but complete project
PPTX
#speakgeek - Angular JS
PDF
Angular Best Practices - Perfomatix
PPTX
Dive into Angular, part 5: Experience
PDF
React Native Firebase
PPTX
AngularJS Fundamentals + WebAPI
PPTX
Angular 2 - a New Hope
Angular 4 - quick view
Fundamental concepts of react js
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Angular 4 for Java Developers
Angular 2 - An Introduction
Introduction To Angular 4 - J2I
AngularJS 2.0: A natural evolvement or a new beginning - Boyan Mihaylov - Cod...
Introduction to React Hooks
AngularJS 2.0
Dive into Angular, part 4: Angular 2.0
Reactjs
Angular 9
Introduction to angular with a simple but complete project
#speakgeek - Angular JS
Angular Best Practices - Perfomatix
Dive into Angular, part 5: Experience
React Native Firebase
AngularJS Fundamentals + WebAPI
Angular 2 - a New Hope
Ad

Similar to What are the components in React? (20)

PPTX
React & Redux for noobs
PPTX
Introduction to React JS.pptx
PPTX
Introduction to ReactJS UI Web Dev .pptx
PPTX
How to create components in react js
PPTX
Dyanaimcs of business and economics unit 2
PDF
Copy of React_JS_Notes.pdf
PDF
How to create components in ReactJS_.pdf
PPTX
Unit 2 Fundamentals of React -------.pptx
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
PPTX
React - Start learning today
PDF
Intro to React
PDF
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
PPTX
ReactJS_Components_Presentation for freshers
PDF
React Interview Question & Answers PDF By ScholarHat
PPTX
react-slides.pptx
DOCX
React Components.docx
PPTX
React Class Components vs Functional Components: Which is Better?
PDF
Getting Started with React, When You’re an Angular Developer
React & Redux for noobs
Introduction to React JS.pptx
Introduction to ReactJS UI Web Dev .pptx
How to create components in react js
Dyanaimcs of business and economics unit 2
Copy of React_JS_Notes.pdf
How to create components in ReactJS_.pdf
Unit 2 Fundamentals of React -------.pptx
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React - Start learning today
Intro to React
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
ReactJS_Components_Presentation for freshers
React Interview Question & Answers PDF By ScholarHat
react-slides.pptx
React Components.docx
React Class Components vs Functional Components: Which is Better?
Getting Started with React, When You’re an Angular Developer
Ad

More from BOSC Tech Labs (20)

PDF
How Computer Vision Powers AI-Driven Process Optimization in Manufacturing.pdf
PDF
Top 10 Ways Computer Vision is Shaping Manufacturing Process.pdf
PDF
Top Computer Vision Opportunities and Challenges for 2024.pdf
PDF
How Computer Vision Is Changing the Entertainment Industry.pdf
PDF
How can Computer Vision help Manufacturers_.pdf
PDF
Machine Learning_ Advanced Computer Vision and Generative AI Techniques.pdf
PDF
What is Generative AI_ Unpacking the Buzz Around Generative AI Development Co...
PDF
20 Unexplored Use Cases for Generative AI in Customer Service.pdf
PDF
The Role of APIs in Custom Software Development for 2024
PDF
What is Generative AI for Manufacturing Operations_.pdf
PDF
How Gen AI Is Transforming The Customer Service Experience_.pdf
PDF
Transforming Visions into Reality with Generative AI.pdf
PDF
What is ChatGPT, DALL-E, and Generative AI_.pdf
PDF
All You Need To Know About Custom Software Development
PDF
The Most Impactful Custom Software Technologies of 2024
PDF
How Vision AI and Gen AI Can Drive Business Growth_.pdf
PDF
10 Detailed Artificial Intelligence Case Studies 2024 | BOSC TECH
PDF
Computer Vision in 2024 _ All The Things You Need To Know.pdf
PDF
GoRouter_ The Key to Next-Level Routing in Flutter Development.pdf
PDF
5 Key Steps to Successfully Hire Reactjs App Developers.pdf
How Computer Vision Powers AI-Driven Process Optimization in Manufacturing.pdf
Top 10 Ways Computer Vision is Shaping Manufacturing Process.pdf
Top Computer Vision Opportunities and Challenges for 2024.pdf
How Computer Vision Is Changing the Entertainment Industry.pdf
How can Computer Vision help Manufacturers_.pdf
Machine Learning_ Advanced Computer Vision and Generative AI Techniques.pdf
What is Generative AI_ Unpacking the Buzz Around Generative AI Development Co...
20 Unexplored Use Cases for Generative AI in Customer Service.pdf
The Role of APIs in Custom Software Development for 2024
What is Generative AI for Manufacturing Operations_.pdf
How Gen AI Is Transforming The Customer Service Experience_.pdf
Transforming Visions into Reality with Generative AI.pdf
What is ChatGPT, DALL-E, and Generative AI_.pdf
All You Need To Know About Custom Software Development
The Most Impactful Custom Software Technologies of 2024
How Vision AI and Gen AI Can Drive Business Growth_.pdf
10 Detailed Artificial Intelligence Case Studies 2024 | BOSC TECH
Computer Vision in 2024 _ All The Things You Need To Know.pdf
GoRouter_ The Key to Next-Level Routing in Flutter Development.pdf
5 Key Steps to Successfully Hire Reactjs App Developers.pdf

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Empathic Computing: Creating Shared Understanding
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Empathic Computing: Creating Shared Understanding
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
Review of recent advances in non-invasive hemoglobin estimation

What are the components in React?

  • 2. What are the components in React? • We can say that every application you will develop in React will be made up of pieces called components. So, in this article, we will see what are the components in React. • A Component is one of the core building blocks of React. It makes the task of building UIs much easier. You can see a UI broken down into multiple individual pieces called components. Also, you can work on them independently and merge them all in a parent component which will be your final UI.
  • 3. 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 posts 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 return a piece of JSX code that tells what should be rendered on the screen. In React, we mainly have two types of components.
  • 4. 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 Below example shows a valid functional component in React: const Democomponent=()=> { return <h1>Welcome Message!</h1>; }
  • 5. 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 other class components. We can use JavaScript ES6 classes to create class- based components in React. Below example shows a valid class-based component in React:
  • 6. class Democomponent extends React.Component { render(){ return <h1>Welcome Message!</h1>; } }
  • 7. • 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.
  • 8. Rendering Components: • In our previous post on rendering elements in React we have seen how elements initialized 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 the first argument to the ReactDOM.render() method. Below syntax shows how to initialize a component to an element: const elementName = <ComponentName />;
  • 9. In the above syntax, the ComponentName is the name of the user- defined component. The name of a component should always start with a capital letter. This is done to differentiate a component tag with HTML tags. The below example renders a component named Welcome to the screen. Open your index.js file from your project directory, and make the given below changes:
  • 10. • 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") • );
  • 11. 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”.
  • 12. Conclusion: • So, in this article, we have been through what are the components in React. Also, feel free to comment with your suggestions and feedback on the post. Moreover, at BOSC Tech Labs, we have a team of highly experienced React JS developers. They can assist you in developing your customized web app. So contact us to hire experienced ReactJS developers.