Skip to content

Commit

Permalink
Add explanatory comments to StateSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
ygshbht committed Mar 29, 2024
1 parent 7e81480 commit 3f87bf2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/StateSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,26 @@ function StateSelector(
const isFocused = useIsFocused();

useEffect(() => {
// Check if the state selector was opened and no value was selected, triggering onBlur to display an error
if (isFocused && didOpenStateSelector.current) {
didOpenStateSelector.current = false;
if (!stateFromUrl) {
onBlur?.();
}
}

// If no state is selected from the URL, exit the effect early to avoid further processing.
if (!stateFromUrl) {
return;
}

// This will cause the form to revalidate and remove any error related to country name
// If a state is selected, invoke `onInputChange` to update the form and clear any validation errors related to the state selection.
if (onInputChange) {
onInputChange(stateFromUrl);
}

// Clears the `state` parameter from the URL to ensure the component state is driven by the parent component rather than URL parameters.
// This helps prevent issues where the component might not update correctly if the state is controlled by both the parent and the URL.
Navigation.setParams({state: undefined});

// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit 3f87bf2

Please sign in to comment.