Can't set default value of "Select" component inside a module from local storage value

Scenario: I have a module that contains a "Select" component that allows users to select a facility that they want to view various dashboards for. I made the select component a module so that I could re-use the query that populates it across multiple apps. Now, I would like for the initial value of the select component to be whatever the user selected in their previous browser session or what they had selected on a previous dashboard when navigating between dashboards. To me, this smells like a job for local storage.

What I tried:
I added an input to the facility selector module called initialSelection. I set the "Default Value" field of the select component inside the module to {{initialSelection.value}}. In the parent app, I have a query that fires whenever the module output, which is just an integer selectedFacilityId, changes. In the success event handlers of the query, I run a JS query that does:

localStorage.setValue("lastSelectedFacilityId", facilitySelector.selectedFacilityId);

Lastly, I have a variable in the parent app called initialFacilitySelection whose "Initial value" field is set to {{localStorage.values.lastSelectedFacilityId}}. This variable is passed in to the module input called initialSelection as {{initialFacilitySelection.value}}. I never call .setValue() on my variable initalFacilitySelection because I only want it to get set once on page load from whatever was set in local storage from prior.

Even though the local storage value appears to be getting set correctly when the dropdown changes, every time I refresh the page, the query to populate the select component fires and the select gets set to the first query result. If I test my module by setting the test input initialSelection to some facility ID, it renders correctly. But, put the module inside a parent app and it falls apart. My guess is that it's a "sequence of events" issue, like the variable in the parent app not getting set before the module renders, which causes the local storage variable to get set to the first query result and not the facility from the previous page load. Are there any suggestions for how to achieve this? Seems like it should be a pretty simple thing to do, but so far I haven't been able to come up with an approach that works. Thanks!

Welcome to the forum!
Can you post some screenshots of the module set up and the "parent app" - you may be missing a variable to pass into the parent app from the module or vice versa.

I rubber ducked through it and got it working by doing the following:

Change the initial value of my INITIAL_FACILITY_SELECTION to blank, and create a JS query that runs on page load to set the variable's value. Not sure why this is any different than setting the initial value field on the variable, though.


I had to set a delay on the success event handler for my dashboard query that sets the local storage value to avoid any race conditions between setting the initial value of the dropdown and setting the selected value in local storage.

And for extra safety, I set the dashboard query's disable criteria to check to make sure a value has been selected so that it doesn't run while the initial value input is being set.

Sorry for wasting anyone's time, but sometimes you figure stuff out when you type out everything you've tried :-). Hopefully this will help someone in the future.