# Default selecting an option with the Select component

**URL:** <https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828>\
**Category:** Developer Q&A Forum\
**Tags:** strato\
**Created:** [December 29, 2022, 4:29pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828 "2022-12-29T16:29:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [December 29, 2022, 4:29pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/1 "2022-12-29T16:29:54Z")

</div>

I’m using the [Select component](https://developer.dynatrace.com/reference/design-system/preview/forms/Select/) for an app, but although an option is selected by default the initial UI still says “choose an option”.

See attached screenshots for an additional explanation (screenshot 1 with the default state, screenshot 2 is in the ‘clicked’ state without any selection).

What I’m looking for: Have ‘Emerging Threats’ already visible in the drop down when I’m loading the page as it’s selected by default.

 ![Screenshot 2022-12-29 at 17.27.50.png](https://global.discourse-cdn.com/dynatrace/original/3X/3/d/3d44d01d816ef8202eed6a3dbfdea69eb90704fc.png)  
 ![Screenshot 2022-12-29 at 17.27.57.png](https://global.discourse-cdn.com/dynatrace/original/2X/b/b7a81fbd3c29c5cc38a2910cd0d78b068d333aef.png)

---

<div class="post-metadata">

**Author:** ![christian\_barth](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/christian_barth/32/34060_2.png) [@christian\_barth](https://community.dynatrace.com/u/christian_barth)\
**Post date:** [December 30, 2022, 8:16am UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/2 "2022-12-30T08:16:58Z")

</div>

Hi,

The Select component provides a prop called **defaultSelectedId** , which does exactly what you want to achieve.

```javascript
<Select name="choice" defaultSelectedId={['option3']}>
  <SelectOption id="option1" value="first">
    3coresec
  </SelectOption>
  <SelectOption id="option2">feodotracker</SelectOption>
  <SelectOption id="option3">emergingthreats</SelectOption>
</Select>

```

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [January 2, 2023, 10:15am UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/3 "2023-01-02T10:15:52Z")

</div>

Thank you, Christian - is there any documentation which types defaultSelectedId allows? I get following error when I’m following your suggestion

 ![3ene_0-1672654541767.png](https://global.discourse-cdn.com/dynatrace/original/2X/c/c36b2845ccac1b34d729bb0da7c3f5b18b3f7710.png)

Just fyi: if I deleted the whole “defaultSelectedId=…” part, it works just fine (source as an attribute for selectedId works).

---

<div class="post-metadata">

**Author:** ![christian\_barth](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/christian_barth/32/34060_2.png) [@christian\_barth](https://community.dynatrace.com/u/christian_barth)\
**Post date:** [January 2, 2023, 12:52pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/4 "2023-01-02T12:52:12Z")

</div>

Hi,

You can find the documentation of the Props at the very bottom of the page:&nbsp;[Select | Dynatrace Developer](https://developer.dynatrace.com/reference/design-system/preview/forms/Select/#proptable)

In your case, it has to be an Array from type Key, which again is either a string or a number.

The easiest way to find this out is to check the type definitions directly in your IDE:

![christian_barth_0-1672663920598.png](https://global.discourse-cdn.com/dynatrace/original/3X/2/7/27261e41fbe49d82740820d3e15bc13cb5ad7766.png)

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [January 2, 2023, 2:49pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/5 "2023-01-02T14:49:03Z")

</div>

Hi,

my selection array (_sources_) is a string array, but it won’t let me set the defaultID:

 ![3ene_3-1672670716824.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/94235609fb08aa613e5e5f83266500c9b10a9f5e.png)

Here is the whole form, maybe I’m making a rookie mistake:

 ![3ene_4-1672670754137.png](https://global.discourse-cdn.com/dynatrace/original/2X/1/1baec380bc3cf1d70961353dcb395cddfdd06aba.png)

I can’t find a solution for my problem so far - even if I follow your suggestion with the [].

 ![3ene_5-1672670906150.png](https://global.discourse-cdn.com/dynatrace/original/3X/f/6/f6541ae13a87c19a42f038abc4f16a2ebd8cecc8.png)

Any other idea what I could try?

React.useState\<SelectedKeys | null\>(null);

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [January 2, 2023, 3:16pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/6 "2023-01-02T15:16:48Z")

</div>

Solved it!

It might be worth to look into the data types that are needed for _defaultSelectedId_, but here is how I could preselect a value:

1.) I run a useEffect that listens to my selection state (_source_ in my example). It also runs when the page is rendered initially.

2.) If that _source_ var is null (ie on the first render of the site), I set it to the desired preselected state, which preselects it. This way I can also query my result set right away.

I don’t need _defaultSelectedId_&nbsp; at all - my select field looks like this:

![3ene_0-1672672370450.png](https://global.discourse-cdn.com/dynatrace/optimized/3X/b/b/bbef590042fc60b11d2515f2df57fa5006c38ddf_2_690x27.png)

I don’t now for sure, but it seems like defaultSelectedId is tricky to use. I couldn’t find the correct type **for hours.** I tried different casts, putting my array selection into brackets, etc. etc.. Documentation should be improved on that end.

---

<div class="post-metadata">

**Author:** ![christian\_barth](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/christian_barth/32/34060_2.png) [@christian\_barth](https://community.dynatrace.com/u/christian_barth)\
**Post date:** [January 2, 2023, 3:51pm UTC](https://community.dynatrace.com/t/default-selecting-an-option-with-the-select-component/37828/7 "2023-01-02T15:51:43Z")

</div>

Another straightforward solution to your problem would be setting the default value of the state variable to your default selection value:

```javascript
const [selected, setSelected] = useState<SelectedKeys | null>(["c"]);

```
