I have to useState declarations:
const [selectedFromYear, setSelectedFromYear] = useState(1900);
const [toYearOptions, setToYearOptions] = useState([])
My component is populating the array years to bind in select options, great, but upon someone selecting say 2015 and this setting selectedtedFromYear I need to repopulate the available toYearOptions to remove any prior to 2015.
I have useEffect listening to selectedFromYear but how do I access/change toYearOptions inside this useEffect?
2
Answers
If what you are doing is just computing options based on
selectedFromYear
only, then its better to useuseMemo
instead of state.You can access and update the toYearOptions state variable inside the useEffect by simply calling the setToYearOptions function.