Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughts on a createElectronRouter API to support v6.4 data APIs #7

Open
offirgolan opened this issue Nov 11, 2022 · 7 comments
Open

Comments

@offirgolan
Copy link
Contributor

馃憢 Hi!

v6.4 of react router introduced data APIs that can only be used via the createBrowserRouter, createHashRouter, and createMemoryRouter.

Thoughts on having this library support a similar wrapper around createHashRouter?

@offirgolan
Copy link
Contributor Author

Following up on this. This is what I'm currently doing:

import type { FC, ReactNode } from 'react';
import { useMemo } from 'react';
import {
  RouterProvider,
  createHashRouter,
  createRoutesFromChildren,
} from 'react-router-dom';

type Routes = Record<string, ReactNode>;

type AppRouterProps = {
  routes: Routes;
};

function getRouteForCurrentWindow(routes: Routes) {
  const selectAllSlashes = /\//g;
  const windowId = window.location.hash
    .split(selectAllSlashes)?.[1]
    ?.toLowerCase();
  const found = Object.keys(routes).find(
    (key) => key.toLowerCase() === windowId
  );

  return found
    ? {
        basename: `/${windowId}`,
        element: routes[found],
      }
    : null;
}

const AppRouter: FC<AppRouterProps> = ({ routes }) => {
  const router = useMemo(() => {
    const route = getRouteForCurrentWindow(routes);

    return route
      ? createHashRouter(createRoutesFromChildren(route.element), {
          basename: route.basename,
        })
      : null;
  }, [routes]);

  return router ? <RouterProvider router={router} /> : null;
};

export { AppRouter, type AppRouterProps };

@daltonmenezes
Copy link
Owner

Hi @offirgolan I think I don't quite understand which APIs you are referring to. Could you please talk more about it and include links, so I can take a look? Currently, the way it is, what's the issue using both libs?

@offirgolan
Copy link
Contributor Author

@daltonmenezes this is documented here

Screen Shot 2022-11-21 at 9 57 53 AM

@daltonmenezes
Copy link
Owner

Thanks, @offirgolan . Have you had the following type issues?
Captura de tela de 2022-11-24 21-12-27

@daltonmenezes
Copy link
Owner

@offirgolan Ok, the type issue was in v6.4.0, bumping to v6.4.3 solves. 馃

@daltonmenezes
Copy link
Owner

@offirgolan using react-router-dom v6.6.0, but we need a electron-router-dom breaking-change to make it work only from that and higher versions. 馃

output.mp4

@offirgolan
Copy link
Contributor Author

@daltonmenezes thanks for taking the time to look into this 馃槃

I think a new major version release for this breaking change makes sense since it will be following react-router-dom's new API recommendations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants