How to create ordered and unordered list in react js?
December 01, 2024Hi Friends π,
Welcome To aHoisting!
To create ordered and unordered list in react js, you have to use the <ol>
tag in ordered and the <ul>
tag used in unordered. It will create ordered and unordered lists in react js.
Today, I am going to show you, how to create ordered and unordered list in react js.
Table of contents
- Install and create a new React app.
- Import react component.
- Create a Component.
Letβs start with the first step.
Step 1: Install and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import react component.
After installing, you have to import your React component.
import React from "react";
import "./App.css";
Step 3: Create a Component.
You can use <ol className="ordered-list">
and <ul className="unordered-list">
to create ordered and unordered list in react js.
<div>
<h2>Ordered Tech Stack</h2>
<ol className="ordered-list">
{techStack.map((tech, index) => (
<li key={index}>{tech}</li>
))}
</ol>
<h2>Unordered Hobbies</h2>
<ul className="unordered-list">
{hobbies.map((hobby, index) => (
<li key={index}>{hobby}</li>
))}
</ul>
</div>
Create ordered and unordered list example.
The below code is an example of a React. You have to import react
and use <ol>
tag in ordered and the <ul>
tag used in unordered to create ordered and unordered list in react js.
App.js
import React from "react";
import "./App.css";
const StyledLists = () => {
const techStack = ["React", "JavaScript", "CSS"];
const hobbies = ["Reading", "Gaming", "Traveling"];
return (
<div>
<h2>Ordered Tech Stack</h2>
<ol className="ordered-list">
{techStack.map((tech, index) => (
<li key={index}>{tech}</li>
))}
</ol>
<h2>Unordered Hobbies</h2>
<ul className="unordered-list">
{hobbies.map((hobby, index) => (
<li key={index}>{hobby}</li>
))}
</ul>
</div>
);
};
export default StyledLists;
App.css
.ordered-list {
list-style-type: decimal; /* Numbers for ordered list */
margin: 10px 0;
padding: 0 20px;
}
.unordered-list {
list-style-type: disc; /* Bullets for unordered list */
margin: 10px 0;
padding: 0 20px;
}
li {
margin: 5px 0; /* Space between items */
font-size: 16px;
color: #333; /* Text color */
}
In the above code example, I have used the <ol>
and <ul>
to create ordered and unordered list in react js.
Check the output of the above code.
All the best π