I have a modal that changes out Select components from Mantine UI based on user input.
When I click on Select by County I want the MultiSelect to be disabled by default. (Done)
When the user selects a state I’m filtering through a json file of county data and saving that to state using useState.
Once a state has been selected I want the County MultiSelect to no longer be disabled and to update the selection options.
This all works fine if I define the County MultiSelect in the return of the function.
I originally had the Select and MultiSelect defined inside of the component but outside of the return as follows:
const hucSelect = (
<Select
label='Watershed Layers'
placeholder='Select One'
data={[
{ value: 'huc2', label: 'HUC2' },
{ value: 'huc4', label: 'HUC4' },
{ value: 'huc6', label: 'HUC6' },
{ value: 'huc8', label: 'HUC8' },
]}
/>
);
const countySelect = (
<MultiSelect
label='County'
disabled={countiesDisabled}
data={filteredCounties}
/>
);
const stateSelect = (
<Select
label='States'
placeholder='Select One'
data={states}
onChange={(_value) => filterCounties(_value)}
/>
);
Then I was using a switch statement inside of useEffect to update another state variable based on which radio button (aoiChoice) the user selected as follows:
useEffect(() => {
switch (aoiChoice) {
case 'state':
setSelectOptions([stateSelect]);
break;
case 'county':
const options = [stateSelect, countySelect];
setSelectOptions(options);
break;
default:
setSelectOptions([hucSelect]);
break;
}
}, [aoiChoice]);
Then in the return statement where the JSX is, I was inserting selectOptions to the DOM as follows:
{selectOptions.map((option) => {
return option;
})}
Doing this however would not update the disabled attribute or the select options. How do I go about updating those components that are defined outside of the return statement?