I have a function to disable a dropdown until selections have been made in 2 other dropdowns. It is not enabling the dropdown properly I believe due to some asynchronous issues with useState
.
const [homeSelect, setHomeSelect] = useState('Home');
const [hedgeSelect, setHedgeSelect] = useState('Hedge');
const [symbolSelect, setSymbolSelect] = useState('1');
const handleHome = (event) => {
setHomeSelect(event.target.value);
exchange_change();
};
const handleHedge = (event) => {
setHedgeSelect(event.target.value);
exchange_change();
};
const handleSymbol = (event) => {
setSymbolSelect(event.target.value);
};
const exchange_change = () => {
if (homeSelect != 'Home' && hedgeSelect != 'Hedge'){
//enable the symbol dropdown
setDisabled(false);
} else {
//disable the select exchanges dropdown
setDisabled(true);
}
}
<FormControl dense>
<TextField
id="standard-select-currency"
select
label="Home"
className={classes.textField}
value={homeSelect}
onChange={handleHome}
SelectProps={{
native: true,
}}
helperText="Please select exchange"
>
{home.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</TextField>
</FormControl>
<FormControl dense>
<TextField
id="standard-select-currency"
select
label="Hedge"
className={classes.textField}
value={hedgeSelect}
onChange={handleHedge}
SelectProps={{
native: true,
}}
helperText="Please select exchange"
>
{hedge.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</TextField>
</FormControl>
<FormControl dense>
<TextField
id="standard-select-currency"
select
label="Symbols"
className={classes.textField}
value={symbolSelect}
onChange={handleSymbol}
disabled={disabled}
SelectProps={{
native: true,
}}
helperText="Please select the exchanges"
>
{symbols.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</TextField>
</FormControl>
home
and hedge
dropdowns require selections before enabling the symbol
dropdown. The error is symbol
dropdown won't enable until after home
and hedge
are selected more than once.
How can I change the methods I'm using to use useReducer
instead (if this will fix the state updating issue)? I tried reading about it but don't understand what my switch cases would be.
Any help is appreciated, thank you.