-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add router abstraction and TanStack Router adapter #11102
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
base: next
Are you sure you want to change the base?
Conversation
djhi
left a comment
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.
Awesome work 💪
react-adminstill has a dependency onreact-routerso users that want TanStack Router will have both routers installed. This is probably a breaking change for some people though.- TanStack router stories conflict with each other. If you navigate from some deep link in one to another story, you may end up with an empty screen.
- We should probably split tan stack related code in multiple files, only re-export the provider from
ra-coreand make the rest available under a sub-path export (https://github.com/colinhacks/zshy/?tab=readme-ov-file#2-specify-your-entrypoints-in-packagejsonzshy)
| import { tanStackRouterProvider } from 'ra-core'; | ||
| const { Route } = tanStackRouterProvider; |
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.
We could have a sub-export path to make this more natural: import { Route } from 'ra-core/tanstack';
|
|
||
| See the [TanStack Router documentation](./TanStackRouter.md) for more details. | ||
|
|
||
| ## Using React-Admin In A Sub Path |
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.
The next chapters are react-router specific and will probably make users wonder how thy apply when using TanStack Router. Maybe we should introduce a ReactRouter page.
|
|
||
| ### History Not Working in Embedded Mode | ||
|
|
||
| When embedding react-admin in an existing TanStack Router app, make sure both routers use the same history type. If your parent app uses hash history, react-admin will work correctly. If it uses browser history, set up your routes accordingly. |
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.
How does one setup their routes accordingly?
|
|
||
| ### History Not Working in Embedded Mode | ||
|
|
||
| When embedding ra-core in an existing TanStack Router app, make sure both routers use the same history type. If your parent app uses hash history, ra-core will work correctly. If it uses browser history, set up your routes accordingly. |
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.
Same
| const PostCreate = () => { | ||
| const navigate = useNavigate(); | ||
| return ( | ||
| <div style={{ padding: 20 }}> | ||
| <h2>Create Post</h2> | ||
| <form> | ||
| <div> | ||
| <label>Title:</label> | ||
| <input /> | ||
| </div> | ||
| <div> | ||
| <label>Body:</label> | ||
| <textarea /> | ||
| </div> | ||
| </form> | ||
| <button onClick={() => navigate('/posts')}>Cancel</button> | ||
| </div> | ||
| ); | ||
| }; |
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.
A real create would be better here, it feels buggy otherwise
Problem
React-admin is tied to react-router. But the two main frameworks used to build new apps today are Next.js and TanStack Start, both NOT using react-router. Although it's possible to integrate RA with Next.js (by opting out of SSR and using a HashRouter), it's cumbersome. It's impossible to do it with TanStack Start.
Solution
Add a routing abstraction layer to allow react-admin to use another router.
Add a TanStack Router implementation to check that the abstraction works for at least 2 routers.
How To Test
React-admin should use react-router by default, and nothing should be broken. The demo apps should work as before.
A demo app powered by TanStack Router was added:
http://localhost:9010/?path=/story/react-admin-frameworks-tanstack--full-app
It contains CRUD routes and custom routes, as well as sub-routes (TabbedForm) and blocker (useWarnWhenUnsavedChanges). All routing features should work, and are covered by integration tests.
The same demo app can be tested in a second story, where it's embedded into an existing TanStack router app:
http://localhost:9010/?path=/story/react-admin-frameworks-tanstack--embedded
In this case, react-admin doesn't create a router but adds its own routes.
Note: these stories don't reset their route properly, so navigating from one to the other will fail. Refresh the browser when opening one of TanStack router's stories.
Reviewing this PR
This is a large PR, so I rebased the history to allow reviewers to read it commit-by-commit.
The tests should pass at the end of each commit.
Additional Checks
masterfor a bugfix or a documentation fix, ornextfor a feature