Tuesday, November 19, 2024
Google search engine
HomeLanguagesHow to use HTML tag in ReactJS ?

How to use HTML tag in ReactJS ?

HTML <select> tag in react is a common web development component used for dynamic form input or as a select menu. It provides multiple selection options in the form of a dropdown menu. It enables the web page to render a select box with the options. The <select>  tag is used as an outer element and the <option> element is nested within the <select> tag for defining options in a list.

Approach for using <select> tag

To achieve this functionality, we will use the “App.js” file and component which will be available by default in the project structure. In our “App.js” component, we will write the main logic. We will use HTML <select> tag and <option> tag. A constant array will be created which will have all the options. Under <select> tag we will iterate the “options” array to display drop-down options. On the <select> tag, we will use the “onChange” property which will hold a reference to the “onOptionChangeHandler” function. This function will get triggered whenever we change the value of the dropdown.

Steps to create the application

Step 1: Create a React.js application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. folder name, move into that directory using the following command:

cd foldername

Project Structure

Screenshot-from-2023-10-06-12-56-14

Example: In this example we will create a dropdown selection input with some options present in the given options array.

Javascript




// App.js
 
import React, { useState } from "react";
import "./App.css";
 
const App = () => {
    const [data, setData] = useState(undefined);
 
    const options = [
        "HTML",
        "CSS",
        "JavaScript",
        "React",
        "Redux",
    ];
    const onOptionChangeHandler = (event) => {
        setData(event.target.value);
        console.log(
            "User Selected Value - ",
            event.target.value
        );
    };
    return (
        <center>
            <h1 className="neveropen"> neveropen</h1>
            <h3>HTML select tag in React js</h3>
 
            <select onChange={onOptionChangeHandler}>
                <option>Please choose one option</option>
                {options.map((option, index) => {
                    return (
                        <option key={index}>
                            {option}
                        </option>
                    );
                })}
            </select>
            <h3>You selected: {data} </h3>
        </center>
    );
};
 
export default App;


CSS




/* App.css */
.App {
    text-align: center;
}
 
.neveropen {
    color: green;
}


Step to Run Application: To run the application, execute the below command from the root directory of the project:

npm start

Output: Your web application will be live on “http://localhost:3000”. Now click on the dropdown to select any value. 

Peek-2023-10-06-12-48

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments