I have a select list (from react-select library). How to force the first item of the seasons array to be selected by default and how to carry out the renderMatches function depending on that automatic choice? My not-working solution:
<Select
onChange = {(e) => renderMatches(e)}
value = {seasons[0]}
options = {seasons}
/>
CodePudding user response:
is <Select />
a component that you have created?
if Yes I will need some more info to answer your question, if No are you using a library? which one?
On the other hand, you should probably have a selected
state or something that goes into value={selected}
and you can set the default value of it in the componentDidMount
or in useEffect
hook.
CodePudding user response:
You can try the defaultValue
prop for the react-select component, that fixes the default value on render. Here is an example of that:
https://stackblitz.com/edit/react-ryvdpq
After that, the value will be set to whatever you set the value prop to. I might be missing something else, but from the code I can't really tell what you are trying to accomplish with the renderMatches
function.
CodePudding user response:
You can do so by converting the first value of array as
let seasonValue = {
value: seasons[0].YourValue,
label: seasons[0].YourLabel,
};
this.setState({ value: seasonValue });
and you can use can use this value
<Select
onChange = {(e) => renderMatches(e)}
value = {value}
options = {seasons}
/>
or check the documentation here