-
-
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
Conversation
🦋 Changeset detectedLatest commit: 3a07532 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Note to self- let's make the error message a bit clearer with a |
e6a5b31
to
7f68b82
Compare
enum DataRouterStateHook { | ||
UseFetchers = "useFetchers", | ||
UseScrollRestoration = "useScrollRestoration", | ||
} |
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
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; | ||
} |
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.
All hooks should use the internal hooks to get access to the contexts, so that we get consistent error messaging across the board
A few folks have messaged in discord trying to use
ScrollRestoration
and other data-router-only hooks insideBrowserRouter
so I went though the new hooks and added the<docs-warning>
to them with a link to "Picking a Router".I left out a few hooks like
useLoaderData
,useActionData
since they seemed self-explanatory?I also updated the implementation to leverage internal private
useDataRouterContext
anduseDataRouterState
hooks that will print a better error message if called inBrowserRouter
:For example: