-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
Fix react-admin requires custom routers to be data routers #9723
Conversation
The reimplentation of the `warnWhenUnsavedChanges` feature with react-router's `useBlocker` created a breaking change. As `useBlocker` throws an error when not called in a DataRouter, and since we cannot call hooks conditionally, this meant that react-admin MUST be used inside a data router. By moving the `warnWhenUnsavedChanges` logic from the `useAugmentedForm` hook to a custom component, we can now call the component conditionally - and avoid calling it when not inside a data router.
import { | ||
FormProvider, | ||
FieldValues, | ||
UseFormProps, | ||
SubmitHandler, | ||
} from 'react-hook-form'; | ||
import { | ||
UNSAFE_DataRouterContext, |
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.
Unfortunately react-router doesn't expose the DataRouterContext, see remix-run/react-router#11347
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.
Other than that, LGTM! 🙂
docs/AccordionForm.md
Outdated
@@ -351,7 +351,7 @@ export const TagEdit = () => ( | |||
); | |||
``` | |||
|
|||
**Warning**: This feature only works if you have a dependency on react-router 6.3.0 **at most**. The react-router team disabled this possibility in react-router 6.4, so `warnWhenUnsavedChanges` will silently fail with react-router 6.4 or later. | |||
**Note**: Due to limitations in react-router, this feature only works if you use the default router provided by react-admin, or if you use a Data Router. |
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.
nitpick: I'd add a link to react-router's documentation for those not knowing what a Data Router is
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.
you're right, added.
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
The reimplentation of the
warnWhenUnsavedChanges
feature with react-router'suseBlocker
in #9657 created a breaking change. AsuseBlocker
throws an error when not called in a DataRouter, and since we cannot call hooks conditionally, this meant that react-admin MUST be used inside a data router.By moving the
warnWhenUnsavedChanges
logic from theuseAugmentedForm
hook to a custom component, we can now call the component conditionally - and avoid calling it when not inside a data router.