fix: preserve navigation state in setSearchParams and add replace option#225
Merged
Conversation
setSearchParams performed a replace navigation without passing state,
wiping per-route state stored on the current entry (via setState /
setStateSync). It now preserves the current entry's state on replace
navigations by default.
It also accepts a new second argument:
setSearchParams(params, { replace?: boolean; state?: unknown }).
replace defaults to true (previous behavior); replace: false pushes a
new entry so the change can be backed out of, and an explicit state
option takes precedence in both cases.
Closes #208
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XyJBmbRYRVoU5WLYxUs8N6
The entry state is an internal container for per-route state, and there is no public API to read raw entry state back, so exposing a raw state option would only let callers clobber route state. Keep the replace option and the state-preservation behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XyJBmbRYRVoU5WLYxUs8N6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #208
Bug fix
setSearchParamsperformed a replace navigation without passingstate, so the new entry's state — including the internal__routeStateswritten bysetState/setStateSync— was silently wiped whenever a query param changed. The setter now reads the current entry's state fromRouterContextand passes it through on replace navigations.New option
The setter accepts an optional second argument:
replace: true(default): replaces the current entry and preserves its navigation state — previous behavior, minus the state loss.replace: false: pushes a new entry so the change can be backed out of with the browser's Back button. The new entry starts without state, consistent with every other push navigation.The
SetSearchParamsOptionstype is exported from the package entry point.The
stateoption suggested in the issue was deliberately left out: the entry state is an internal container for per-route state and there is no public API to read raw entry state back, so exposing it would only let callers clobber route state.Tests & docs
setStateSync) and for pushing viareplace: false.useSearchParamsdocs (ApiHooksPage) and addedSetSearchParamsOptionsto the types reference.🤖 Generated with Claude Code
https://claude.ai/code/session_01XyJBmbRYRVoU5WLYxUs8N6
Generated by Claude Code