I am retrieving a set of values from apex through JS, I want to preselect the the first option in the Lightning-combobox (the retrieved values are different for different users). I used @track variable and tried to set the value onLoad using connectedcallback. Below is the code snippet
HTML
<lightning-layout-item padding="around-small">
<lightning-combobox
name="objPicklist"
label="Objects"
value={selectedObj}
options={objects}
onchange={displayObjRecords} >
</lightning-combobox>
JS file
@track selectedObj;
objects = [];
connectedCallback(event) {
optionList({
userID : this.currentUser,
})
.then(result=>{
var i;
for(i = 0; i < result.length; i++){
const option = {
label : result[i],
value : result[i]
};
this.objects = [...this.objects, option];
}
//SETTING UP THE VALUE HERE.
this.selectedObj = this.objects[0].value;
})
.catch(error => {
this.error = error;
console.log('Error : '+ JSON.stringify(this.error));
})
}
I would like to default the first option retrieved in the lightning combobox. How can I do it in LWC?