-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
docs: add better docs and console errors for data router features #9311
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"react-router": patch | ||
"react-router-dom": patch | ||
--- | ||
|
||
docs: Enhance console error messages for invalid usage of data router hooks |
This file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -642,6 +642,35 @@ if (__DEV__) { | |
//#region Hooks | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
enum DataRouterHook { | ||
UseScrollRestoration = "useScrollRestoration", | ||
UseSubmitImpl = "useSubmitImpl", | ||
UseFetcher = "useFetcher", | ||
} | ||
|
||
enum DataRouterStateHook { | ||
UseFetchers = "useFetchers", | ||
UseScrollRestoration = "useScrollRestoration", | ||
} | ||
|
||
function getDataRouterConsoleError( | ||
hookName: DataRouterHook | DataRouterStateHook | ||
) { | ||
return `${hookName} must be used within a data router. See https://reactrouter.com/en/main/routers/picking-a-router.`; | ||
} | ||
|
||
function useDataRouterContext(hookName: DataRouterHook) { | ||
let ctx = React.useContext(DataRouterContext); | ||
invariant(ctx, getDataRouterConsoleError(hookName)); | ||
return ctx; | ||
} | ||
|
||
function useDataRouterState(hookName: DataRouterStateHook) { | ||
let state = React.useContext(DataRouterStateContext); | ||
invariant(state, getDataRouterConsoleError(hookName)); | ||
return state; | ||
} | ||
Comment on lines
+662
to
+672
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All hooks should use the internal hooks to get access to the contexts, so that we get consistent error messaging across the board |
||
|
||
/** | ||
* Handles the click behavior for router `<Link>` components. This is useful if | ||
* you need to create custom `<Link>` components with the same click behavior we | ||
|
@@ -789,12 +818,7 @@ export function useSubmit(): SubmitFunction { | |
} | ||
|
||
function useSubmitImpl(fetcherKey?: string, routeId?: string): SubmitFunction { | ||
let dataRouterContext = React.useContext(DataRouterContext); | ||
invariant( | ||
dataRouterContext, | ||
"useSubmitImpl must be used within a Data Router" | ||
); | ||
let { router } = dataRouterContext; | ||
let { router } = useDataRouterContext(DataRouterHook.UseSubmitImpl); | ||
let defaultAction = useFormAction(); | ||
|
||
return React.useCallback( | ||
|
@@ -909,9 +933,7 @@ export type FetcherWithComponents<TData> = Fetcher<TData> & { | |
* for any interaction that stays on the same page. | ||
*/ | ||
export function useFetcher<TData = any>(): FetcherWithComponents<TData> { | ||
let dataRouterContext = React.useContext(DataRouterContext); | ||
invariant(dataRouterContext, `useFetcher must be used within a Data Router`); | ||
let { router } = dataRouterContext; | ||
let { router } = useDataRouterContext(DataRouterHook.UseFetcher); | ||
|
||
let route = React.useContext(RouteContext); | ||
invariant(route, `useFetcher must be used inside a RouteContext`); | ||
|
@@ -967,8 +989,7 @@ export function useFetcher<TData = any>(): FetcherWithComponents<TData> { | |
* routes that need to provide pending/optimistic UI regarding the fetch. | ||
*/ | ||
export function useFetchers(): Fetcher[] { | ||
let state = React.useContext(DataRouterStateContext); | ||
invariant(state, `useFetchers must be used within a DataRouterStateContext`); | ||
let state = useDataRouterState(DataRouterStateHook.UseFetchers); | ||
return [...state.fetchers.values()]; | ||
} | ||
|
||
|
@@ -985,22 +1006,13 @@ function useScrollRestoration({ | |
getKey?: GetScrollRestorationKeyFunction; | ||
storageKey?: string; | ||
} = {}) { | ||
let { router } = useDataRouterContext(DataRouterHook.UseScrollRestoration); | ||
let { restoreScrollPosition, preventScrollReset } = useDataRouterState( | ||
DataRouterStateHook.UseScrollRestoration | ||
); | ||
let location = useLocation(); | ||
let matches = useMatches(); | ||
let navigation = useNavigation(); | ||
let dataRouterContext = React.useContext(DataRouterContext); | ||
invariant( | ||
dataRouterContext, | ||
"useScrollRestoration must be used within a DataRouterContext" | ||
); | ||
let { router } = dataRouterContext; | ||
let state = React.useContext(DataRouterStateContext); | ||
|
||
invariant( | ||
router != null && state != null, | ||
"useScrollRestoration must be used within a DataRouterStateContext" | ||
); | ||
let { restoreScrollPosition, preventScrollReset } = state; | ||
|
||
// Trigger manual scroll restoration while we're active | ||
React.useEffect(() => { | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums to represent the hooks that require a data router