@deron
To create a drop-down in React.js, you can follow these steps:
1
|
import React, { useState } from 'react'; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function Dropdown() { const [selectedOption, setSelectedOption] = useState(''); const handleOptionChange = (e) => { setSelectedOption(e.target.value); } return ( <select value={selectedOption} onChange={handleOptionChange}> <option value="">Select an option</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> ); } |
1 2 3 4 5 6 7 |
function App() { return ( <div> <Dropdown /> </div> ); } |
This will render the drop-down with the default option "Select an option" and three additional options: "Option 1", "Option 2", and "Option 3". The selectedOption
state variable will store the currently selected option, and it will update whenever a different option is selected.