SlideShare a Scribd company logo
How to create components in react js
How to create components in ReactJS?
• React uses this JSX format so that we can put HTML and JavaScript
together. So, in this article, we will see how to create components in
ReactJS.
• React components can be defined anywhere in your ReactJS project.
Also, it can be used anywhere in your ReactJS project. There are two
ways to define components in ReactJS. Both are used by a wide range
of programmers. But with the APIS/HOOKs like useState, use context
the functional components are becoming more and more popular.
There are two types of components:
• React functional components
• React class-based component
1) React Functional Components:
This component can be any JavaScript function that returns the HTML.
These functional components can also accept “props”, meaning
properties or data. As these are JavaScript functions or extensions of
JavaScript functions, they can also be created from the ES6 convention
of the arrow function. These functional components can also accept
the props that we can use using JSX syntax and you can also put your
normal JavaScript code before the return statement. One thing to
notice is that there should only be one return per component.
Syntax:
const Greet = (props) => {
const person = props.name;
return (
<div>
<h1>Hello {person}!!</h1>
</div>
)
}
2) React Class-based Components:
• The class-based components also have almost the same features as
React function components. but before we define our class-based
component, we need to import the “React. Component” or extract
the Component like “{Component}” from React.
import React, {Components} from 'react';
Syntax:
import React, { Component } from 'react'
class App extends Component {
render()
return (
<div>
<h1>Hello {this.props.name}</h1>
</div>
)
}
}
Rendering the Component:
• We have successfully made the components, but in order to use them
we need to render them. one can first export their components and
can use them anywhere in the ReactDOM.render method after
importing them. Somewhere in your project in ReactDOM.render().
ReactDOM.render(
<React.StrictMode>
<Greet name="gfg " />
</React.StrictMode>,
document.getElementById('root')
);
Creating React Application:
1) Create a React application using the following command:
npx create-react-app foldername
2) After creating your project folder i.e. folder name, move to
it using the following command:
cd foldername
• Now have a component named “Card” in your App.js. We are going to
use this component later. This component accepts two
arguments/data of “writer” and “receiver”. Then we have one return
which just uses this data of writer and receiver and displays it on a
card. After that, we have made another component named App
which will be used to hold all the components for our Application.
This App has an export default so that we can use it anywhere in our
React App. Hence, we do not have to render it manually. This App is
automatically rendered in your index.js while creating a react app
using npx.
import React from "react";
function Card({ receiver, writer }) {
return (
<div>
<h1 style={{
backgroundColor: "lightblue",
width: "fit-content"
}}>
BOSC Tech Labs
</h1>
<h2 style={{
backgroundColor: "lightgrey",
width: "fit-content"
}}>
We are a team of innovative and passionate engineers who transform your idea into reality.
</h2>
<h3>Your truly, Mr. Nick</h3>
</div>
);
}
export default function App() {
return (
<div>
<Card writer="GFG" receiver="gfg jr" />
</div>
);
}
• Run the application using the following command from the root
directory of the project:
npm start
• Now open your browser and go to http://localhost:3000/, you will see the following output:
Conclusion:
• So, in this article, we have been through how to create components in
ReactJS. 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.
• Article Source: https://bosctechlabs.com/create-components-in-
reactjs/

More Related Content

PPTX
React JS Lecture 10.pptx Our clg lecture
PDF
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
PPTX
Dyanaimcs of business and economics unit 2
PDF
How to create components in ReactJS_.pdf
PPTX
Unit 2 Fundamentals of React -------.pptx
PDF
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
PPTX
React & Redux for noobs
PPTX
What are the components in React?
React JS Lecture 10.pptx Our clg lecture
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Dyanaimcs of business and economics unit 2
How to create components in ReactJS_.pdf
Unit 2 Fundamentals of React -------.pptx
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
React & Redux for noobs
What are the components in React?

Similar to How to create components in react js (20)

PDF
Fundamental concepts of react js
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
PDF
Copy of React_JS_Notes.pdf
PPTX
React - Start learning today
PPT
PPTX
Intro to React.js
ODP
Introduction to ReactJS
PDF
3 Ways to Get Started with a React App in 2024.pdf
PDF
Fundamental Concepts of React JS for Beginners.pdf
PPTX
20171108 PDN HOL React Basics
PDF
React js
PDF
React & Flux Workshop
PPTX
React_Complete.pptx
PDF
Getting Started with React, When You’re an Angular Developer
PPTX
Introduction to ReactJS UI Web Dev .pptx
PPTX
ReactJs presentation
PDF
Stay with React.js in 2020
PDF
REACTJS.pdf
PPTX
Fundamental concepts of react js
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
Copy of React_JS_Notes.pdf
React - Start learning today
Intro to React.js
Introduction to ReactJS
3 Ways to Get Started with a React App in 2024.pdf
Fundamental Concepts of React JS for Beginners.pdf
20171108 PDN HOL React Basics
React js
React & Flux Workshop
React_Complete.pptx
Getting Started with React, When You’re an Angular Developer
Introduction to ReactJS UI Web Dev .pptx
ReactJs presentation
Stay with React.js in 2020
REACTJS.pdf
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
Ad

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Online Work Permit System for Fast Permit Processing
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPT
Introduction Database Management System for Course Database
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
DOCX
The Five Best AI Cover Tools in 2025.docx
ai tools demonstartion for schools and inter college
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Understanding Forklifts - TECH EHS Solution
Online Work Permit System for Fast Permit Processing
A REACT POMODORO TIMER WEB APPLICATION.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
AIRLINE PRICE API | FLIGHT API COST |
Introduction Database Management System for Course Database
How Creative Agencies Leverage Project Management Software.pdf
The Role of Automation and AI in EHS Management for Data Centers.pdf
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Softaken Excel to vCard Converter Software.pdf
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
How to Choose the Right IT Partner for Your Business in Malaysia
Materi-Enum-and-Record-Data-Type (1).pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
The Five Best AI Cover Tools in 2025.docx

How to create components in react js

  • 2. How to create components in ReactJS? • React uses this JSX format so that we can put HTML and JavaScript together. So, in this article, we will see how to create components in ReactJS. • React components can be defined anywhere in your ReactJS project. Also, it can be used anywhere in your ReactJS project. There are two ways to define components in ReactJS. Both are used by a wide range of programmers. But with the APIS/HOOKs like useState, use context the functional components are becoming more and more popular. There are two types of components:
  • 3. • React functional components • React class-based component 1) React Functional Components: This component can be any JavaScript function that returns the HTML. These functional components can also accept “props”, meaning properties or data. As these are JavaScript functions or extensions of JavaScript functions, they can also be created from the ES6 convention of the arrow function. These functional components can also accept the props that we can use using JSX syntax and you can also put your normal JavaScript code before the return statement. One thing to notice is that there should only be one return per component.
  • 4. Syntax: const Greet = (props) => { const person = props.name; return ( <div> <h1>Hello {person}!!</h1> </div> ) }
  • 5. 2) React Class-based Components: • The class-based components also have almost the same features as React function components. but before we define our class-based component, we need to import the “React. Component” or extract the Component like “{Component}” from React. import React, {Components} from 'react';
  • 6. Syntax: import React, { Component } from 'react' class App extends Component { render() return ( <div> <h1>Hello {this.props.name}</h1> </div> ) } }
  • 7. Rendering the Component: • We have successfully made the components, but in order to use them we need to render them. one can first export their components and can use them anywhere in the ReactDOM.render method after importing them. Somewhere in your project in ReactDOM.render(). ReactDOM.render( <React.StrictMode> <Greet name="gfg " /> </React.StrictMode>, document.getElementById('root') );
  • 8. Creating React Application: 1) Create a React application using the following command: npx create-react-app foldername 2) After creating your project folder i.e. folder name, move to it using the following command: cd foldername
  • 9. • Now have a component named “Card” in your App.js. We are going to use this component later. This component accepts two arguments/data of “writer” and “receiver”. Then we have one return which just uses this data of writer and receiver and displays it on a card. After that, we have made another component named App which will be used to hold all the components for our Application. This App has an export default so that we can use it anywhere in our React App. Hence, we do not have to render it manually. This App is automatically rendered in your index.js while creating a react app using npx.
  • 10. import React from "react"; function Card({ receiver, writer }) { return ( <div> <h1 style={{ backgroundColor: "lightblue", width: "fit-content" }}> BOSC Tech Labs </h1> <h2 style={{ backgroundColor: "lightgrey", width: "fit-content" }}>
  • 11. We are a team of innovative and passionate engineers who transform your idea into reality. </h2> <h3>Your truly, Mr. Nick</h3> </div> ); } export default function App() { return ( <div> <Card writer="GFG" receiver="gfg jr" /> </div> ); }
  • 12. • Run the application using the following command from the root directory of the project: npm start • Now open your browser and go to http://localhost:3000/, you will see the following output:
  • 13. Conclusion: • So, in this article, we have been through how to create components in ReactJS. 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. • Article Source: https://bosctechlabs.com/create-components-in- reactjs/