First, let me say that I searched a lot about the problem, but I could not solve my problem.
I have a specific value that I want to be displayed in the view in the select box by default.
select code :
<Select
options={getMainCategory.data.map((item) => {
return { value: item.id, label: item.categoryName };
})}
defaultValue={getMainCategory.data.find((x)=>{
const c = x.id === mainparentId;
return { value: c.id, label: c.categoryName };
})}
onChange={onOptionChange}
/>
I did it with the above code but it didn’t work. I would be grateful if you could guide me?
3
Answers
The issue in your code is you are using
defaultValue
function, which must bestring
,number
,object
. In your example you have to usevalue
property as following:you can use it in this way
import React, { useState } from ‘react’;
In this example, the defaultValue prop is set to the option object that matches the value ‘banana’. When the component first renders, the ‘Banana’ option will be selected by default.
You can change the default value to any other option by finding its corresponding option object and passing it as the defaultValue.