-
-
Notifications
You must be signed in to change notification settings - Fork 897
feat(react-router): Add useHistoryState hook for type-safe history state management #3967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
naoya7076
wants to merge
23
commits into
TanStack:main
Choose a base branch
from
naoya7076:add-usehistorystate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,100
−13
Conversation
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
This commit introduces a new `validateState` function to handle state validation within the router. It supports various validation methods, including standard validation and parsing, and integrates with route options to validate state during routing.
This commit introduces the TStateValidator type across various routing components, enhancing the validation capabilities for state management. The changes include updates to the createFileRoute and Route classes, as well as adjustments in related files to support the new state validation feature.
This commit introduces the `useHistoryState` hook and related types across various routing components, improving state management capabilities. Updates include modifications to the Route and createRoute functions to support the new state handling feature.
This commit introduces a new section in the BaseTanStackRouterDevtoolsPanel to display state parameters when available.
This commit introduces the `useHistoryState` hook along with its associated types, enhancing the state management capabilities within the router. The new implementation allows for more flexible state handling and selection options, improving overall functionality.
This commit introduces new TypeScript types related to the `useHistoryState` hook, improving type safety and flexibility in state management within the router. The new types facilitate better state selection and handling options, enhancing the overall functionality of the routing system.
…roved state selection This commit refactoring the state selection logic to utilize `useRouterState`.
…resolution This commit modifies the `ResolveUseHistoryState` and ` UseHistoryStateResult` types
…ryState This commit updates the `useHistoryState` hook to utilize `useLocation` for state management instead of `useRouterState`, improving the clarity and efficiency of state selection logic.
…tools panel This commit introduces a new function to filter out internal properties (keys starting with '__') from the router state before it is displayed in the BaseTanStackRouterDevtoolsPanel.
…nction in useHistoryState
…n useHistoryState
…router Router class
…TanStackRouterDevtoolsPanel
…toolsPanel by merging strict state
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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.
Remaining tasks
useHistoryState
Dev tool update
Currently displaying all states, but some are unnecessary.
Conflict resolution
Resolve the conflict in packages/react-router/src/router.ts.
Unit test fix
Review and fix packages/react-router/tests/useHistoryState.test.tsx.
Overview
This PR introduces the new
useHistoryState
hook and related functionality to enable type-safe history state management in TanStack Router.#975 (comment)
Background
In the current implementation of TanStack Router, to pass type-safe state (History State) between routes that isn't part of the path or params, we need to define types globally. This leads to two main issues:
For example:
Additionally, when migrating from react-router to TanStack Router, developers want to be able to handle state in a type-safe manner, similar to how they could with react-router:
#284
This PR addresses these issues by adding route-specific, type-safe history state management.
Changes
useHistoryState
hook export in the main package indexFileRoute
class to support state validation withTStateValidator
generic parameterexamples/react/basic-history-state
demonstrating usage patternsmessage
in state objectsKey features of useHistoryState
Example usage
Users can define state validation on routes:
Then use it in components: