Answer the question
In order to leave comments, you need to log in
How to set default in select?
Hello everyone, here's the thing, I'm working on a project and I have a problem. How to set the default year 2020 in select. Of course, I could use the react-select library, but I want without the library.
import React from "react";
const data = [
{year: '2017', id: 1},
{year: '2018', id: 2},
{year: '2019', id: 3},
{year: '2020', id: 4},
{year: '2021', id: 5},
]
function App() {
return (
<>
<select>
{
data.map(item => (
<option key={item.id}>{item.year}</option>
))
}
</select>
</>
)
}
export default App
Answer the question
In order to leave comments, you need to log in
data.map(item => (
<option key={item.id} selected={item.year === 2020}>{item.year}</option>
))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question