oedotme / generouted

Generated file-based routes for Vite
https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer
MIT License
1.02k stars 47 forks source link

feat: add memory and hash routing to react-router #162

Closed Jaouan closed 5 months ago

Jaouan commented 5 months ago

Hey! πŸ‘‹

First of all, thank you for this library, which is an interesting and enjoyable accelerator to use.

On one of my projects using react-router, I needed to use memory-routing (via createMemoryRouter). Unfortunately, it didn't seem possible with Generouted. Here's a PR that adds <HashRoutes /> and <MemoryRoutes /> to the react-router implementation.

If you see any adjustments that need to be made, I'll be happy to do them. 🫑

Have a great day ✌️

oedotme commented 5 months ago

Hey @Jaouan, thanks for your message and submitting this PR.

Tbh I don't see the need to export HashRoutes or MemoryRoutes as the routes object (not Routes component) was exported for this customization purpose. The default app entry with the default browser Routes looks like that:

import { createRoot } from 'react-dom/client'
import { Routes } from '@generouted/react-router'

const container = document.getElementById('app')!
createRoot(container).render(<Routes />)

In order to use HashRouter with the current exports, it would be exactly the same as what you exported in this PR:

import { createRoot } from 'react-dom/client'
import { RouterProvider, createHashRouter } from 'react-router-dom'
import { routes } from '@generouted/react-router'

const Routes = () => <RouterProvider router={createHashRouter(routes)} />

const container = document.getElementById('app')!
createRoot(container).render(<Routes />)

I think we can close this PR, and I'm thinking to include this point in a (new) FAQ section on the docs.

Jaouan commented 5 months ago

Hi, I forgot that β€˜routes’ is also exported. 🫣 Your example is enough to handle memory/hash routing. πŸ‘

oedotme commented 5 months ago

No worries, glad that helped!