D
D
Dosya2021-11-03 08:34:06
React
Dosya, 2021-11-03 08:34:06

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

1 answer(s)
A
Alexander Makarov, 2021-11-03
@Dasihub

data.map(item => (
          <option key={item.id} selected={item.year === 2020}>{item.year}</option>
        ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question